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
- Rows: Each row is a record in your dataset.
- Columns: Each column represents a field or attribute.
- DataFrame: The overall table combines rows and columns into a structured dataset.
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:
- Pandas DataFrame: Perfect for small to medium data that fits into your laptop’s memory. It runs everything in-memory on a single machine and provides a rich, Python-friendly API.
- PySpark DataFrame: Designed for big data. It distributes data across a cluster and uses Spark’s JVM-based engine under the hood. It’s optimized for parallel processing, and its API feels more like SQL (e.g., select, filter, groupBy).
Key differences:
- Execution Environment: Pandas runs locally; PySpark runs on a cluster.
- Data Size: Pandas handles MB to a few GB; PySpark scales to GB–TB.
- Performance: Pandas can slow down or run out of memory on huge data; PySpark is built for big data.
- API Style: Pandas is Pythonic and flexible; PySpark is SQL-like and functional.
When to Use Each
- Choose Pandas for exploratory analysis or projects where your data fits comfortably into RAM and you want a quick, interactive workflow.
- Choose PySpark for ETL jobs, large-scale data engineering, or machine learning tasks that require distributed processing.
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
- about pyspark
- text diagram
- Apache Spark Runtime Architecture
- Introduction to RDD
- Actions vs Transformations
- Lazy Evaluation in PySpark
All tutorials · Try the free PySpark compiler · Practice challenges