Ciaren

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
regionamountorder_id
North1201001
South891002
North2101003
South421004
4 rows · 3 cols
Group By + Aggregate (group_by=region, sum amount, count order_id)
After
regionamountorder_id
North3302
South1312
2 rows · 3 cols

Configuration

Config keyTypeRequiredDescription
group_bystring[]YesGrouping columns
aggregationsobjectYes{ "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.
  • count counts non-null values in the column; nunique counts distinct values.
  • To reshape grouped results into a matrix, use Pivot.

See also