PivotFromColumns

PivotFromColumns[tab,cols{vars, vals}]

pivots to make a longer table with the columns cols replaced by two columns, vars containing the column names and vals containing the values from cols.

PivotFromColumns[tab,{prule1,prule2,}]

pivots on multiple sets of columns specified by the pivot rules prulei.

Details

  • PivotFromColumns is also known as gather and pivot longer.
  • PivotFromColumns is typically used as a structural data-cleaning operation, where some columns are not variables but values of variables. PivotFromColumns pivots the role of variable and value by gathering the values from several columns, making the resulting tabular object longer.
  • PivotToColumns is effectively the inverse of PivotFromColumns.

Examples

open allclose all

Basic Examples  (1)

Pivot the keys of several columns into a column called "day" and their values into a column called "temp":

PivotToColumns can then be used to separate the columns for high and low temperatures:

Scope  (2)

Take a Tabular object of populations of the G7 countries:

Pivot the country column names and population values into new columns "Country" and "Population":

Take tabular data of weekend temperature and sun information:

Pivot the "high" and "low" columns of temperature values:

Pivot "sunrise" with "high" and "sunset" with "low":

Alternatively, pivot "sunrise" with "low" and "sunset" with "high":

When the sets of different value columns have different lengths, both names and values are filled in with Missing:

Applications  (2)

Create a Tabular object with measurements of soil pH at three depth levels of 0, 30 and 80 meters:

Pivot the depth values to create "depth" and "pH" columns and then sort by depth:

Find mean pH for each depth:

Take rectangular data organized by years and months:

Pivot the month columns into a "month" column and "data" column:

It is now easy to compute yearly means:

Properties & Relations  (3)

Take tabular data of weekend temperature and sun information:

Pivot two sets of columns at the same time:

Changing the order of the rules will change the order of columns:

Take tabular data of weekend temperature and sun information:

Simultaneous pivoting is not equivalent to using the same rules in consecutive separate steps:

Use PivotFromColumns on a Tabular object:

PivotToColumns is effectively the inverse of PivotFromColumns:

Remove the common part in ExtendedKey to recover the original object:

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

Text

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

CMS

Wolfram Language. 2025. "PivotFromColumns." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/PivotFromColumns.html.

APA

Wolfram Language. (2025). PivotFromColumns. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/PivotFromColumns.html

BibTeX

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

BibLaTeX

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