The full answer
Transformations describe how to build a new DataFrame or RDD from an existing one and are lazy; actions ask for a result (returned to the driver or written to storage) and make Spark execute all the transformations they depend on.
Transformations
- Return a new DataFrame or RDD; the original is unchanged, because they are immutable.
- Are recorded in a plan, not executed.
- Come in two kinds:
- Narrow: each output partition depends on one input partition, so no data moves between executors. Examples:
select,filter/where,withColumn,drop,map,flatMap,union,coalesce. - Wide: output partitions need data from many input partitions, so Spark shuffles data across the network and starts a new stage. Examples:
groupBy().agg(),join(unless broadcast),distinct,orderBy,repartition,reduceByKey.
- Narrow: each output partition depends on one input partition, so no data moves between executors. Examples:
Actions
- Trigger a job.
- Return values to the driver:
count(),collect(),first(),take(n),show(),toPandas(). - Or write output:
write.save(),write.saveAsTable(),foreach(),foreachPartition().
Example
from pyspark.sql import functions as F
orders = spark.read.parquet("/data/orders")
summary = (orders
.filter(F.col("amount") > 0) # narrow
.withColumn("year", F.year("order_date")) # narrow
.groupBy("year", "region") # wide: shuffle
.agg(F.sum("amount").alias("revenue")))
summary.show(5) # action: job runs
summary.write.mode("overwrite").parquet("/out/rev") # action: runs again
Nothing executes until show(). The second action recomputes everything unless summary is cached.
Nuances
collect()andtoPandas()bring all data to the driver; use them only on small results. Prefershow(),take()or writing to storage.- Wide transformations are expensive because of the shuffle; filter and aggregate early to shrink what is shuffled, and broadcast small tables in joins.
cache()andpersist()are lazy too: they mark data to be kept, and it is stored by the next action.- Calling
explain()or readingdf.schemadoesn't run a job, but schema inference on CSV or JSON does read data. - In notebooks, display commands are actions, so each displayed cell can run a job.
How to tell them apart quickly If the method returns a DataFrame or RDD, it is a transformation; if it returns a Python value, a list of rows or nothing (because it writes somewhere), it is an action. In the Spark UI, every action appears as one or more jobs, so a notebook that shows dozens of jobs for one cell usually has more actions than intended.
Transformations define what to compute; actions decide when, which is what lets Spark optimise the whole plan before running it.
Last updated 16 October 2026
Sources