Pivot
Pivot — pivot
Reshape long → wide.
Use cases
- Turn
monthrows intoJan/Feb/… columns of totals. - Build a cross-tab of category × metric.
What it does
Pivot spreads the unique values of one column (columns) out into new columns,
filling each cell by aggregating the values column for that row key.
Before
| region | month | amount |
|---|---|---|
| North | Jan | 100 |
| North | Feb | 150 |
| South | Jan | 80 |
| South | Feb | 200 |
4 rows · 3 cols
Pivot (index=region, columns=month, values=amount, aggfunc=sum)
After
| region | Jannew | Febnew |
|---|---|---|
| North | 100 | 150 |
| South | 80 | 200 |
2 rows · 3 cols
Configuration
| Config key | Type | Required | Description |
|---|---|---|---|
index | string | string[] | Yes | Row key(s) |
columns | string | Yes | Column whose values become new columns |
values | string | Yes | Column to aggregate into the cells |
aggfunc | string | No | Aggregation (default sum): one of sum, mean, min, max, median, first, last, count — restricted to functions both the pandas and polars exports support |
Generated Python code
df_2 = df_1.pivot_table(index='region', columns='month', values='amount', aggfunc='sum').reset_index()
Tips & common mistakes
aggfuncresolves collisions. When multiple rows share the same index/column pair, they're combined with this function (sum, mean, count, …).- New column names come from the values found in
columnsat run time. - To go the other way (wide → long), use Unpivot.