Arman Pouyaei Start a conversation
Lesson 19  ·  Oct 28, 2026
Data Engineering Fundamentals

Nothing runs until you say so: transformations vs. actions

Yesterday you met the DataFrame. Today comes the one idea that explains most PySpark surprises: transformations and actions are two different kinds of operation. Transformations build a plan; actions run it. Until an action shows up, Spark has done no real work at all — no matter how many lines of code you wrote.

The code

from pyspark.sql import SparkSession

spark = SparkSession.builder.appName("weather-stations").getOrCreate()

data = [
    ("NYC", 40.71, -74.01, 28.5),
    ("Princeton", 40.36, -74.66, 26.0),
    ("Boston", 42.36, -71.06, 22.5),
]
df = spark.createDataFrame(data, ["city", "lat", "lon", "temp_c"])

# Transformations: these only build the plan. Nothing runs yet.
filtered = df.filter(df.temp_c > 25)
hot = filtered.withColumn("temp_f", filtered.temp_c * 9 / 5 + 32)

# Action: NOW Spark runs the whole plan, end to end.
hot.show()

filter() and withColumn() are transformations: each returns a new DataFrame describing the next step, but neither touches the data. show() is an action: it demands a result, so Spark takes the full chain — read, filter, add column — optimizes it, and executes it. Other common actions: count() (how many rows?), collect() (bring all rows back to your program), and write (save the result, e.g. df.write.parquet(...)).

In practice

Laziness is a feature, not a quirk. Because Spark sees the whole plan before running anything, its optimizer can push the filter down, drop columns you never use, and skip work entirely — optimizations that are impossible if every step runs the moment you write it. It also explains two classic beginner puzzles: a typo in a column name may only surface when the action runs, and calling two actions on the same DataFrame can run the plan twice (unless you cache it — a later topic).

In your world: a pipeline over millions of storm-sensor rows is a long chain of transformations — filter to a region, add a derived column, join to station metadata — followed by a single write action at the end. The chain is cheap to build and easy to read; the action is where the cluster actually earns its keep. One warning from the field: collect() pulls every row into your driver's memory, so on a huge table it can crash your job. Peek with show() or take(5) instead.

Python bridge: pandas is eager — df[df.temp_c > 25] filters immediately, and each step costs real time on real data. PySpark is lazy — the equivalent filter() just writes down the intention. Think of a PySpark DataFrame as a recipe card, not a finished dish: transformations edit the recipe, and only an action starts cooking.

Further reading

Transformations and Actions in Spark Explained — Spark Playground (a short, beginner-friendly walkthrough: which operations are lazy, which trigger a job, and why laziness lets Spark optimize)

Try it

Practice question: starting from the df above, write code that keeps only stations with temp_c of at least 25, selects just the city and temp_c columns, and shows the result. Then answer: which line in your code is the one that actually makes Spark run?

Show answer
warm = df.filter(df.temp_c >= 25).select("city", "temp_c")
warm.show()

Result: only show() makes Spark run. filter() and select() are transformations — they just extend the plan. When show() fires, Spark executes the whole chain and prints:

+---------+------+
|     city|temp_c|
+---------+------+
|      NYC|  28.5|
|Princeton|  26.0|
+---------+------+

Boston (22.5) is filtered out, and only the two selected columns appear.

← All posts