Introduction to Pyspark DataFrames

Spark tutorial · PySpark.in

Introduction to DataFrames in PySpark

 PySpark DataFrame as a giant, distributed spreadsheet. According to the Spark documentation, a DataFrame is a dataset organized into named columns—much like a table in a relational database or a data frame in R or Python spark.apache.org. The difference is that this “spreadsheet” is automatically split across multiple machines, so you can work with gigabytes or terabytes of data without running out of memory.

Rows, Columns, and Table Analogy

Because PySpark DataFrames are distributed, they break your data into partitions. Each partition is processed in parallel by different executors, allowing Spark to handle data that would overwhelm a single machine.

PySpark vs. Pandas DataFrames

Imagine you have two tools for working with tables:

Key differences:

When to Use Each

 Examples

PySpark DataFrame

```
from pyspark.sql import SparkSession
spark = SparkSession.builder.appName("DataFrameExample").getOrCreate()
data = [("Alice", 25), ("Bob", 30)]
columns = ["Name", "Age"]
df = spark.createDataFrame(data, columns)
df.show()
```

Pandas DataFrame

```

import pandas as pd
data = [("Alice", 25), ("Bob", 30)]
columns = ["Name", "Age"]
df = pd.DataFrame(data, columns=columns)
print(df)

```

More Spark tutorials

All tutorials · Try the free PySpark compiler · Practice challenges