DataFrame
| Also known as | Data frame, Spark DataFrame |
|---|---|
| Field | Data processing APIs |
| Introduced | Spark 1.3.0 (DataFrame API in Apache Spark) |
| Key parts | Named, typed columns (a schema); rows; a logical plan in Spark |
| Open source implementations | Apache Spark (PySpark, Scala, Java, R), pandas, Polars |
| Related | Dataset (Spark), RDD, Spark Connect, Apache Arrow, User-defined function (Spark) |
A DataFrame is a table-like data structure made of named columns, each with a data type, and rows. In Apache Spark, a DataFrame is a distributed collection of rows organized into named columns, conceptually equivalent to a table in a relational database, and it is the main API of Spark SQL in Python, Scala, Java and R.[1] The same word names the in-memory, single-process tables of pandas and Polars, which share the tabular shape but differ in where the data lives and when work is done. This page explains the Spark DataFrame and compares it with those two.
What is a DataFrame in Apache Spark?
A Spark DataFrame is a Dataset organized into named columns. In Scala, DataFrame is a type alias of Dataset[Row]; in Java it is written Dataset<Row>; Python and R have only the DataFrame.[1] DataFrames can be built from files, tables, external databases or existing RDDs.[1]
A Spark DataFrame does not hold its data in the driver. Internally it represents a logical plan that describes how to produce the data. Transformations such as select, filter and groupBy return new DataFrames, and nothing runs until an action such as count, show or a write is called. Then Spark's optimizer turns the logical plan into a physical plan that runs in parallel on executors; explain() shows both plans.[2]
With Spark Connect, a PySpark or Scala DataFrame lives in a thin client: the client sends unresolved logical plans to the Spark server over gRPC and receives results as Apache Arrow batches.[3]
from pyspark.sql import SparkSession, functions as F
spark = SparkSession.builder.getOrCreate()
orders = spark.read.parquet("s3://bucket/orders/") # no data is read yet
daily = (orders
.filter(F.col("status") == "shipped")
.groupBy("order_date")
.agg(F.sum("amount").alias("revenue"))) # still only a plan
daily.explain() # prints the physical plan
daily.show(5) # action: Spark runs the job on the executors
The same query in Spark SQL:
SELECT order_date, SUM(amount) AS revenue
FROM parquet.`s3://bucket/orders/`
WHERE status = 'shipped'
GROUP BY order_date;
How do Spark DataFrames work with table formats?
In an open lakehouse, Spark DataFrames are the usual way to read and write tables in Delta Lake, Apache Iceberg and Apache Parquet files. For example, the Delta Lake quick start writes a DataFrame with data.write.format("delta").save(path) and reads it back with spark.read.format("delta").load(path).[4]
Spark DataFrame vs pandas vs Polars
| Apache Spark DataFrame | pandas DataFrame | Polars DataFrame | |
|---|---|---|---|
| Where the data lives | Partitions spread across executors in a cluster; the DataFrame object is a plan[2] | In memory; pandas is designed for in-memory analytics[5] | In memory on one machine[6] |
| Evaluation | Lazy: transformations build a plan, actions run it[2] | Eager[7] | Eager DataFrame and lazy LazyFrame; in many cases the eager API calls the lazy engine[8]
|
| Query optimizer | Spark's query optimizer turns the logical plan into a physical plan[2] | None (operations run as called)[7] | Query planner with predicate and projection pushdown in lazy mode[8] |
| Parallelism | Tasks across many executors and machines[1] | Not built in; the pandas docs point to other libraries for parallel and distributed work[5] | Multi-threaded across the CPU cores of one machine[6] |
| Row index | No row labels | Labeled row and column axes[9] | None; rows are addressed by integer position[7] |
| Memory format | A binary internal format produced by encoders[2]; Arrow for data exchange with Python[10] | NumPy arrays by default; columns can be backed by PyArrow[11][7] | Arrow memory specification, with its own compute implementation[6][7] |
| Languages | Python, Scala, Java, R, SQL[1] | Python | Core in Rust; Python, R and NodeJS APIs[6] |
| Latest version (checked 2026-11-23) | PySpark 4.3.0[12] | pandas 3.1.1[13] | Polars 2.1.0[14] |
How do you move data between Spark, pandas and Arrow?
DataFrame.toPandas() collects a Spark DataFrame to the driver as a pandas DataFrame, and SparkSession.createDataFrame() accepts a pandas DataFrame. Both use Apache Arrow when spark.sql.execution.arrow.pyspark.enabled is true, and since Spark 4.2 it is true by default.[10][15] Since Spark 4.0, DataFrame.toArrow() returns a PyArrow Table and createDataFrame() accepts one; a Polars DataFrame can be exchanged through Arrow this way.[10][6]
Both toPandas() and toArrow() bring every row to the driver, so the Spark documentation advises using them only on a small subset of the data.[10]
The pandas API on Spark (pyspark.pandas), added in Spark 3.2.0, offers a pandas-like API whose operations run on Spark DataFrames.[16]
History
Spark 1.3.0 added the DataFrame API, described in the release notes as an evolution of the RDD API with named fields and schema information, for Python, Scala and Java.[17] It replaced SchemaRDD.[18] Spark 2.0.0 unified DataFrame and Dataset in Scala and Java, making DataFrame a type alias for Dataset[Row].[19]
See also
References
- ↑ 1.0 1.1 1.2 1.3 1.4 Spark SQL, DataFrames and Datasets Guide (Spark 4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
- ↑ 2.0 2.1 2.2 2.3 2.4 Dataset.scala (tag v4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
- ↑ Spark Connect Overview (Spark 4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
- ↑ Quickstart. Delta Lake.Retrieved 2026-11-23.
- ↑ 5.0 5.1 Scaling to large datasets (pandas 3.1.1). pandas.Retrieved 2026-11-23.
- ↑ 6.0 6.1 6.2 6.3 6.4 Polars user guide. Polars.Retrieved 2026-11-23.
- ↑ 7.0 7.1 7.2 7.3 7.4 Coming from Pandas. Polars.Retrieved 2026-11-23.
- ↑ 8.0 8.1 Lazy API. Polars.Retrieved 2026-11-23.
- ↑ pandas.DataFrame (pandas 3.1.1). pandas.Retrieved 2026-11-23.
- ↑ 10.0 10.1 10.2 10.3 Apache Arrow in PySpark (arrow_pandas.rst at v4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
- ↑ PyArrow Functionality (pandas 3.1.1). pandas.Retrieved 2026-11-23.
- ↑ pyspark. PyPI.Retrieved 2026-11-23.
- ↑ pandas. PyPI.Retrieved 2026-11-23.
- ↑ polars. PyPI.Retrieved 2026-11-23.
- ↑ Upgrading PySpark (pyspark_upgrade.rst at v4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
- ↑ Spark Release 3.2.0. Apache Software Foundation.Retrieved 2026-11-23.
- ↑ Spark Release 1.3.0. Apache Software Foundation.Retrieved 2026-11-23.
- ↑ Migration Guide: SQL, Datasets and DataFrame (Spark 4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
- ↑ Spark Release 2.0.0. Apache Software Foundation.Retrieved 2026-11-23.