Interactive Lab · Data & pandas

GROUP BY is three moves in a trench coat

It looks like one operation, but it's really three: split the rows into groups by a key, apply a function to each group, and combine the answers into one row per group. That's the whole trick behind SQL GROUP BY and pandas groupby. Pick a grouping column and an aggregation below, and watch nine rows collapse, live, in three visible phases.

🎬 Watch the 90-second explainer, then pick a column and an aggregation below ↓

▶ The playground, pick a group key and an aggregation

GROUP BY
AGGREGATE
·

1 SPLIT, tint every source row by the group it lands in

categoryitemqtyprice
↓

2 APPLY, each group's rows reduce under sum

↓

3 · COMBINE, one row per group

categorysum(qty)
input rows 9 → output groups 0 aggregation sum

The code that's actually running, pandas and SQL



    

🧭 Where this fits

GROUP BY is the single most-run operation in analytics: every revenue-by-region dashboard, every DAU count, every funnel metric is a group-and-aggregate. It's also the shape of MapReduce and every data warehouse query plan, understanding collapse-to-one-row-per-key is understanding how Snowflake and BigQuery bill you.

🔮 Predict, then test

Switch the aggregation from SUM to COUNT, the numbers change meaning entirely (running totals become plain tallies of how many rows fell in each group). Now change the grouping column from category to item and, before you look, predict how many output rows you'll get. Hint: it's the number of distinct values in that column. Were you right?

The takeaway

Your new mental model of GROUP BY