Group by + aggregate
Group by + aggregate — groupByAggregate
Group rows and compute aggregates.
Use cases
- Total sales per region, average order value per customer.
- Count distinct products per category (
nunique).
What it does
Collapses multiple rows that share the same group key(s) into a single summary row. All non-grouped, non-aggregated columns are dropped from the result.
Before
| region | amount | order_id |
|---|---|---|
| North | 120 | 1001 |
| South | 89 | 1002 |
| North | 210 | 1003 |
| South | 42 | 1004 |
4 rows · 3 cols
Group By + Aggregate (group_by=region, sum amount, count order_id)
After
| region | amount | order_id |
|---|---|---|
| North | 330 | 2 |
| South | 131 | 2 |
2 rows · 3 cols
Configuration
| Config key | Type | Required | Description |
|---|---|---|---|
group_by | string[] | Yes | Grouping columns |
aggregations | object | Yes | { "col": "func" } |
Aggregation functions: sum, mean, count, min, max, median,
nunique, std, first, last.
Generated Python code
df_2 = df_1.groupby('region').agg({'amount': 'sum'}).reset_index()
Tips & common mistakes
- The result has one row per group; non-grouped, non-aggregated columns are
dropped — list everything you need under
aggregations. countcounts non-null values in the column;nuniquecounts distinct values.- To reshape grouped results into a matrix, use Pivot.