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 all

Basic Examples  (1)

Pivot the weekdays in the "day" column into new columns of temperature values:

Scope  (2)

Pivot a table on two sets of columns:

Pivot a table on two sets of values:

Keep the data for each day adjacent:

Applications  (3)

Sales Data  (1)

Get retail data:

Use PivotToColumns to separate sales for each city:

Create a TemporalData with "Date" used as the "TimeColumn":

The column headers are stored in "MetaInformation":

Visualize the sales time series:

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:

Wolfram Research (2025), PivotToColumns, Wolfram Language function, https://reference.wolfram.com/language/ref/PivotToColumns.html.

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

BibTeX

@misc{reference.wolfram_2024_pivottocolumns, author="Wolfram Research", title="{PivotToColumns}", year="2025", howpublished="\url{https://reference.wolfram.com/language/ref/PivotToColumns.html}", note=[Accessed: 15-January-2025 ]}

BibLaTeX

@online{reference.wolfram_2024_pivottocolumns, organization={Wolfram Research}, title={PivotToColumns}, year={2025}, url={https://reference.wolfram.com/language/ref/PivotToColumns.html}, note=[Accessed: 15-January-2025 ]}