groupBy, every join type and window functions — the patterns behind most pipelines, and which of them shuffle.
groupBy(...).agg(...) collapses rows into one per group. Pass several aggregate functions at once and name each with .alias() — otherwise you get columns called sum(amount) or count(DISTINCT customer_id). count_distinct is the current name; countDistinct is the older alias.
It runs as one shuffle: each task first pre-aggregates its own partition (partial sums and counts), then rows are exchanged by the grouping key so each key's partials meet in one task for the final merge. Distinct counts cost more — Spark must first de-duplicate the (group, value) pairs, which moves far more data than a running total.
from pyspark.sql import functions as F
daily = (orders
.groupBy("country", F.to_date("ts").alias("day"))
.agg(F.sum("amount").alias("revenue"),
F.count("*").alias("orders"),
F.count_distinct("customer_id").alias("buyers"),
F.avg("amount").alias("avg_order"),
F.max("amount").alias("largest_order"))
.filter(F.col("orders") >= 10)) # like SQL HAVINGReach the last step for the run summary: the key takeaways from this lesson.