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.
| region | amount | order_id |
|---|---|---|
| North | 120 | 1001 |
| South | 89 | 1002 |
| North | 210 | 1003 |
| South | 42 | 1004 |
| region | amount | order_id |
|---|---|---|
| North | 330 | 2 |
| South | 131 | 2 |
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, var, prod, first, last. Other names are rejected so a
flow behaves the same on the pandas and Polars engines.
Generated Python code
These exports group by region and sum only amount; the illustration above also counts order_id. pandas sorts the group keys, while the Polars export preserves their first appearance. Rows with a missing key form their own group on both engines, so their values still count.
pandas
df_2 = df_1.groupby('region', dropna=False).agg({'amount': 'sum'}).reset_index()Polars
df_2 = df_1.group_by('region', maintain_order=True).agg(pl.col('amount').sum())Tips & common mistakes
- The result has one row per group; non-grouped, non-aggregated columns are
dropped, so 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.