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.
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.
Examples
open all close allBasic Examples (1)
Scope (2)
Pivot a table on two sets of columns:
table = ToTabular[...]PivotToColumns[table, {"hlm" -> "temp", "horizon" -> "time"}]Pivot a table on two sets of values:
table = ToTabular[...]PivotToColumns[table, "day" -> {"temp", "time"}]Keep the data for each day adjacent:
PivotToColumns[table, {"day" -> "temp", "day" -> "time"}]Applications (4)
Penguins (1)
Take penguin data collected at Palmer Archipelago:
data = ResourceData["Sample Tabular Data: Palmer Penguins"]Use PivotToColumns to split "body_mass" column into two columns based on "sex":
PivotToColumns[data, "sex" -> "body_mass"]Compare using PairedHistogram:
PairedHistogram[% -> "male", % -> "female", ChartLabels -> {"male", "female"}, PlotLabel -> "body mass"]Sales Data (1)
data = Tabular[{...}]Use PivotToColumns to separate sales for each city:
PivotToColumns[data, "City" -> "Sales"]Create a TemporalData with "Date" used as the "TimeColumn":
td = FromTabular[%, "TemporalData", <|"TimeColumn" -> "Date"|>]The column headers are stored in "MetaInformation":
td["MetaInformation"]Visualize the sales time series:
DateListPlot[td, PlotLegends -> Last /@ td["PathNames"]]Life Expectancy (1)
Download a Tabular object from the Wolfram Data Repository:
expect = ResourceData["Sample Tabular Data: Health Expenditure"]Pivot by country so that each row is a year, to compare the data between countries:
PivotToColumns[expect, "Country" -> {"TotalHealthExpenditure", "LifeExpectancy"}]Have the spending and life expectancy columns appear together:
PivotToColumns[expect, {"Country" -> "TotalHealthExpenditure", "Country" -> "LifeExpectancy"}]Lake Mead (1)
Time series of Lake Mead water levels:
ts = TemporalData[TimeSeries, {{{Missing[], Quantity[708.7, "Feet"], Quantity[701.7, "Feet"],
Quantity[752.4, "Feet"], Quantity[806.6, "Feet"], Quantity[909.1, "Feet"],
Quantity[928.4, "Feet"], Quantity[925.9, "Feet"], Quantity[920.8, "Feet"], ... fication[DateObject[{1935, 1, 31}, "Day"], {2024, 6, 30, 0, 0, 0},
"EndOfMonth", "DayRange"]}, 1, {"Continuous", 1}, {"Discrete", 1}, 1,
{ResamplingMethod -> {"Interpolation", InterpolationOrder -> 1}, ValueDimensions -> 1}}, True,
14.2];Create a Tabular object:
tab1 = ToTabular[ts, "TimeSeries"]tab2 = DeleteColumns[TransformColumns[tab1, {"Year" -> Function[DateValue[#Date, "Year"]], "Month" -> Function[DateValue[#Date, "MonthName"]]}], "Date"]Use PivotToColumns to convert into a more compact form for visualization:
tab3 = PivotToColumns[tab2, "Month" -> "Value"]Properties & Relations (1)
Use PivotToColumns on a Tabular object:
table = ToTabular[...]PivotToColumns[table, "day" -> "temp"]Simplify the column keys by removing the common part in ExtendedKey:
RenameColumns[%, ExtendedKey["temp", a_] :> a]Use PivotFromColumns to recover the original Tabular object:
PivotFromColumns[%, {"M", "Tu", "W", "Th", "F"} -> {"day", "temp"}]% === tableHistory
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_2026_pivottocolumns, author="Wolfram Research", title="{PivotToColumns}", year="2025", howpublished="\url{https://reference.wolfram.com/language/ref/PivotToColumns.html}", note=[Accessed: 13-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_pivottocolumns, organization={Wolfram Research}, title={PivotToColumns}, year={2025}, url={https://reference.wolfram.com/language/ref/PivotToColumns.html}, note=[Accessed: 13-June-2026]}