Spark vs Hadoop MapReduce vs pandas
Apache Spark · Spark Fundamentals
Theory
Hadoop MapReduce, Spark and pandas all process tabular data, but they work very differently. MapReduce runs each job as a map phase and a reduce phase and writes results to HDFS on disk between steps, so a five-step pipeline means five rounds of disk writes. Spark builds the whole pipeline as one plan, keeps intermediate data in memory where possible and only writes to disk for shuffles, so iterative and multi-step jobs are much faster. Spark also gives you high-level DataFrames and SQL instead of hand-written mapper and reducer classes.
pandas runs on a single machine and needs the data to fit in that machine's RAM, but it starts instantly and is very convenient for small data. For example, a 200 MB CSV of orders is a pandas job; 500 GB of daily order files is a Spark job. PySpark lets you move between them with toPandas() and spark.createDataFrame(pandas_df), and also ships a pandas-like API (pyspark.pandas).
Why we use it
Interviewers often ask why Spark replaced MapReduce and when you would still pick pandas. Knowing the trade-offs (memory vs disk, single machine vs cluster, startup cost) helps you choose the right tool instead of using Spark for everything.
When to use it
Pick pandas (or Polars/DuckDB) when data fits comfortably in one machine's memory. Pick Spark when data is large, growing, or already in a data lake, or when the job must scale out. New projects rarely choose raw MapReduce today.
How it works
- You have a data processing task
- Does the data fit comfortably in one machine's memory? Yes: Use pandas, Polars or DuckDB. No: Use a distributed engine.
- Is it a new pipeline on a data lake or warehouse? Yes: Use Spark DataFrames or Spark SQL. No: Keep legacy MapReduce only if it already exists.
- Prototype on a sample locally
- Scale out the same Spark code on a cluster
Syntax
pdf = spark_df.toPandas() # Spark -> pandas (collects to driver!)
sdf = spark.createDataFrame(pdf) # pandas -> Spark
import pyspark.pandas as ps # pandas API on SparkExample
from pyspark.sql import SparkSession, functions as F
spark = (SparkSession.builder.master("local[1]")
.config("spark.ui.enabled", "false")
.getOrCreate())
import pandas as pd
# The same question in pandas (one machine) and in PySpark (distributed)
pdf = pd.DataFrame({"region": ["North", "West", "North", "South"],
"amount": [55000.0, 30000.0, 1500.0, 52000.0]})
print(pdf.groupby("region", as_index=False)["amount"].sum())
sdf = spark.createDataFrame(pdf) # pandas -> Spark
(sdf.groupBy("region").agg(F.sum("amount").alias("amount"))
.orderBy("region").show())
small = sdf.filter("amount > 20000").toPandas() # Spark -> pandas (small results only)
print(type(small).__name__, len(small))
# Output:
# region amount
# 0 North 56500.0
# 1 South 52000.0
# 2 West 30000.0
# +------+-------+
# |region| amount|
# +------+-------+
# | North|56500.0|
# | South|52000.0|
# | West|30000.0|
# +------+-------+
#
# DataFrame 3
Key points
- MapReduce writes to disk between every map and reduce step; Spark keeps data in memory where it can
- Spark builds one optimized plan for the whole pipeline instead of separate jobs
- pandas is single-machine and in-memory; great for small data, fails when data outgrows RAM
- toPandas() pulls all rows to the driver: only use it on small results
- Spark has startup and scheduling overhead, so it can be slower than pandas on tiny data