Join first, then summarize: aggregations + joins
Two moves do most of the summarizing work in a PySpark pipeline: join (enrich one table with another) and groupBy + agg (collapse rows into per-group summaries). The pattern is almost always in that order — join first so every row carries its context, then aggregate so the summary is grouped by something meaningful.
The code
from pyspark.sql import SparkSession
from pyspark.sql import functions as F
spark = SparkSession.builder.appName("weather-stations").getOrCreate()
stations = spark.createDataFrame([
(1, "Princeton", "Northeast"),
(2, "NYC", "Northeast"),
(3, "Denver", "West"),
], ["station_id", "city", "region"])
readings = spark.createDataFrame([
(1, 26.0), (1, 28.0), (2, 30.0), (3, 20.0),
], ["station_id", "temp_c"])
# Join: attach each reading's city and region. "station_id" is
# the shared key, "inner" keeps only rows that match on both sides.
joined = readings.join(stations, "station_id", "inner")
# Aggregate: one summary row per region.
summary = joined.groupBy("region").agg(
F.avg("temp_c").alias("avg_temp"),
F.count("*").alias("n_readings"),
)
summary.show()
join() takes two DataFrames, a key, and a join type: readings.join(stations, "station_id", "inner") matches rows where the key is equal on both sides, and inner drops anything with no match (a reading from an unknown station would vanish — a left join would keep it, with null city and region). Then groupBy("region") splits the joined rows into one bucket per region, and agg(...) computes one summary per bucket: F.avg("temp_c") for the mean temperature and F.count("*") for how many readings fed into it. alias() just names the output column. Other handy agg functions: F.sum, F.min, F.max, F.countDistinct.
In practice
This is the shape of a huge share of real pipelines: raw events joined to a small dimension table (stations, regions, device metadata), then rolled up to the level a dashboard or model actually needs. Doing the join first matters — aggregating before the join would leave you with averages per station_id and no region column left to group by. And like yesterday: join and groupBy are transformations, so nothing runs until the action at the end. show() is what makes Spark execute the whole chain.
In your world: millions of storm-sensor readings joined to station metadata, then averaged per region per day, is exactly this pattern at scale — the same two lines, just with a read.parquet(...) source and a write action instead of show(). One thing to watch as data grows: a join can shuffle a lot of data across the cluster, so joining a giant table to a tiny one is cheap, while joining two giant tables on a skewed key is where pipelines slow down.
Python bridge: this is pandas merge followed by groupby: readings.merge(stations, on="station_id").groupby("region")["temp_c"].agg(["mean", "count"]). Same two ideas, same order — PySpark just spells them join and groupBy(...).agg(...), and runs them across a cluster instead of in memory.
Further reading
Join and Aggregate DataFrames in PySpark — Spark Code Hub (a short walkthrough of joining two DataFrames and then grouping and averaging, with the same join + groupBy + agg shape as today)
Try it
Practice question: using the same stations and readings DataFrames, write code that joins them and then finds the highest temperature recorded in each city (one row per city). Show the result.
Show answer
joined = readings.join(stations, "station_id", "inner")
city_max = joined.groupBy("city").agg(
F.max("temp_c").alias("max_temp"),
)
city_max.show()
Result:
+---------+--------+ | city|max_temp| +---------+--------+ |Princeton| 28.0| | NYC| 30.0| | Denver| 20.0| +---------+--------+
Princeton takes the max of its two readings (26.0 and 28.0); NYC and Denver have one reading each. Grouping by city instead of region is the only change — the join stays exactly the same.