Interactive Lab · Data & pandas
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
1 SPLIT, tint every source row by the group it lands in
| category | item | qty | price |
|---|
2 APPLY, each group's rows reduce under sum
3 · COMBINE, one row per group
| category | sum(qty) |
|---|
The code that's actually running, pandas and SQL
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.
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
sum totals, mean averages, count tallies, max picks the extreme. Same rows, wildly different meaning.GROUP BY and pandas groupby, learn it once, use it everywhere.