Skip to the page
Tecyfy

PySpark interview question · easy

What is the difference between transformations and actions in Spark?

The short answerTransformations describe a new dataset derived from an existing one (select, filter, join, groupBy) and are lazy: Spark only records them in a plan.

The full answer

Transformations describe a new dataset derived from an existing one (select, filter, join, groupBy) and are lazy: Spark only records them in a plan. Actions (count, collect, show, write) ask for a result, which makes Spark optimise the plan and run a job on the cluster.

Transformations

  • Return a new DataFrame or RDD; the original is unchanged (data is immutable).
  • Lazy: nothing is read or computed yet.
  • Two kinds:
    • Narrow: each output partition depends on one input partition (select, filter, withColumn, map, union). They run together in one stage without moving data.
    • Wide: output partitions depend on many input partitions (groupBy().agg(), join without broadcast, distinct, orderBy, repartition). They need a shuffle and start a new stage.

Actions

  • Return a value to the driver (count, collect, first, take, show, toPandas) or write data out (write.save, write.saveAsTable, foreach).
  • Trigger a job: the scheduler turns the plan into stages and tasks.
  • Each action runs the whole lineage again unless intermediate results are cached.
from pyspark.sql import functions as F

orders = spark.read.parquet("/data/orders")                 # nothing read yet
big = orders.filter(F.col("amount") > 1000)                 # transformation (narrow)
by_country = big.groupBy("country").count()                 # transformation (wide)
by_country.show()                                           # action: job runs
by_country.write.mode("overwrite").parquet("/out/big_by_country")   # action: runs again

The second action recomputes everything from the source. If both are needed, by_country.cache() before the first action avoids that, at the cost of memory.

Why laziness helps

  • Spark sees the whole pipeline before running it, so it can push the filter into the Parquet scan, read only the needed columns and fuse operations together.
  • No intermediate results are materialised unless needed.
  • Lineage lets lost partitions be recomputed after failures.

Common surprises

  • Errors in a transformation (a bad cast, a missing file) appear only when an action runs, in a different line from where they were written. Since Spark 4, ANSI mode makes invalid casts raise errors, which makes this more visible.
  • collect() and toPandas() bring all rows to the driver and can exhaust its memory; use limit, take or write the result instead.
  • Calling count() just to "check" data runs a full job.
  • Some operations behave like actions behind the scenes: reading CSV with inferSchema, or checkpoint(), run jobs immediately.

Quick classification

TransformationsActions
select, withColumn, filter, where, dropshow, count, collect, take, first
join, union, distinct, dropDuplicateswrite...save, saveAsTable, insertInto
groupBy().agg(), orderBy, repartition, coalesceforeach, foreachPartition, toPandas
RDD: map, flatMap, reduceByKeyRDD: reduce, saveAsTextFile, countByKey

Last updated 16 October 2026

Sources

Where each fact comes from

  1. 1RDD programming guide
  2. 2PySpark API reference