PySpark vs pandas: when do you use each?
Source: PySpark documentation
pandas runs on one machine, eagerly, and is ideal while data fits in memory: quick analysis, small transforms. PySpark distributes the work, is lazy and optimised, and handles data far larger than one machine, at the cost of cluster start-up and more complex debugging.
pandas runs on one machine, eagerly, and is ideal while data fits in memory: quick analysis, small transforms. PySpark distributes the work, is lazy and optimised, and handles data far larger than one machine, at the cost of cluster start-up and more complex debugging. toPandas() brings Spark data to the driver, so use it only on small results.
The middle options
- Single-node engines (Polars, DuckDB): they run on one machine but are often faster and lighter on memory than pandas, and they can process files larger than RAM. For data in the tens of GB that fits on one big machine, they are often simpler and cheaper than a cluster.
- pandas API on Spark (
pyspark.pandas): pandas-like code that runs on Spark, so a team that knows pandas can scale out. It is not identical to pandas, and some operations (such as ones that need a global order) are expensive on a cluster.
Rough size guide
This is a rule of thumb, not a benchmark, and it depends on your RAM, file format and the work you do. If the data comfortably fits in RAM on one machine (remember pandas often needs several times the file size in memory), use pandas or Polars/DuckDB. If it is larger than one machine, or it already lives in a lakehouse and many people share it, use Spark.
toPandas(), Arrow and driver memory
toPandas() collects every row to the driver process. If the result is large it can fail with an out-of-memory error, or stop at the spark.driver.maxResultSize limit (default 1g). Reduce the data first (filter, aggregate, limit, sample). Setting spark.sql.execution.arrow.pyspark.enabled to true makes the transfer between Spark and pandas columnar and much faster, but it does not remove the memory limit: all the data must still fit on the driver.
pandas
- One machine, eager
- Data fits in memory
- Quick analysis
PySpark
- Distributed, lazy, optimised
- Data far beyond one machine
toPandas()only on small results
See the concept: Spark shufflegroupBy moves every record to the partition that owns its key
Input partition 0
Input partition 1
Input partition 2
Stage 1: narrow transformations run in place
Key points
- single machine vs distributed
- eager vs lazy
- toPandas on small data
Common mistakes
- Using Spark for a few hundred MB of data and paying for cluster start-up and complexity.
- Using pandas on data that does not fit in memory, then fighting out-of-memory errors.
- Calling
toPandas()on a large DataFrame, which collects everything to the driver. - Assuming pandas API on Spark behaves exactly like pandas for ordering and index operations.
- Forgetting that pandas code runs eagerly, while Spark code is lazy and only runs on an action.
Best practices
- Start with the simplest tool that fits the data and the team: pandas or Polars/DuckDB first, Spark when you outgrow one machine.
- Filter and aggregate in Spark, then call
toPandas()only on the small result. - Enable Arrow (
spark.sql.execution.arrow.pyspark.enabled) when converting between Spark and pandas. - Test logic on a sample locally, then run on the full data in Spark.
- Prefer built-in Spark functions over Python UDFs for speed.
Likely follow-ups
- Why does
toPandas()run the driver out of memory? - When would you choose DuckDB or Polars over Spark?
- What is the pandas API on Spark and how does it differ from pandas?
- What does Arrow change when you convert a Spark DataFrame to pandas?
- How would you move a pandas pipeline to Spark when the data grows?