Ciaren

Pivot

Pivot — pivot

Reshape long → wide.

Use cases

  • Turn month rows into Jan/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
regionmonthamount
NorthJan100
NorthFeb150
SouthJan80
SouthFeb200
4 rows · 3 cols
Pivot (index=region, columns=month, values=amount, aggfunc=sum)
After
regionJannewFebnew
North100150
South80200
2 rows · 3 cols

Configuration

Config keyTypeRequiredDescription
indexstring | string[]YesRow key(s)
columnsstringYesColumn whose values become new columns
valuesstringYesColumn to aggregate into the cells
aggfuncstringNoAggregation (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

  • aggfunc resolves 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 columns at run time.
  • To go the other way (wide → long), use Unpivot.

See also