PivotToColumns
PivotToColumns[tab,varcol->valcol]
pivots the tabular object tab to another table with new column keys taken from the column varcol and values taken from the column valcol.
PivotToColumns[tab,varcol{valcol1,valcol2,…}]
creates columns with values from all valcoli for each value from varcol.
PivotToColumns[tab,{prule1,prule2,…}]
pivots tab using several pivot rules prulei.
Details
- PivotToColumns is also known as spread and pivot wider.
- PivotToColumns is typically used as a structural data cleaning operation, where an observation is scattered across multiple rows. PivotToColumns pivots the role of variable and value by spreading the values from one column into several columns.
- The column keys of the result have the form ExtendedKey[valcol,vari], where vari are the values of the column varcol.
- PivotFromColumns is effectively the inverse of PivotToColumns.
- PivotToColumns takes the same options as Tabular.
Examples
open allclose allScope (2)
Applications (3)
Sales Data (1)
Use PivotToColumns to separate sales for each city:
Create a TemporalData with "Date" used as the "TimeColumn":
Life Expectancy (1)
Download a Tabular object from the Wolfram Data Repository:
Pivot by country so that each row is a year, to compare the data between countries:
Have the spending and life expectancy columns appear together:
Lake Mead (1)
Time series of Lake Mead water levels:
Create a Tabular object:
Use PivotToColumns to convert into a more compact form for visualization:
Properties & Relations (1)
Use PivotToColumns on a Tabular object:
Simplify the column keys by removing the common part in ExtendedKey:
Use PivotFromColumns to recover the original Tabular object:
Text
Wolfram Research (2025), PivotToColumns, Wolfram Language function, https://reference.wolfram.com/language/ref/PivotToColumns.html.
CMS
Wolfram Language. 2025. "PivotToColumns." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/PivotToColumns.html.
APA
Wolfram Language. (2025). PivotToColumns. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/PivotToColumns.html