PivotFromColumns
✖
PivotFromColumns
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.
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 allBasic Examples (1)Summary of the most common use cases
Pivot the keys of several columns into a column called "day" and their values into a column called "temp":

https://wolfram.com/xid/0cf2c7rbtteublk-e14cod


https://wolfram.com/xid/0cf2c7rbtteublk-lh8ctc

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

https://wolfram.com/xid/0cf2c7rbtteublk-77ynol

Scope (2)Survey of the scope of standard use cases
Take a Tabular object of populations of the G7 countries:

https://wolfram.com/xid/0cf2c7rbtteublk-h935jg
Pivot the country column names and population values into new columns "Country" and "Population":

https://wolfram.com/xid/0cf2c7rbtteublk-8ic9vn

Take tabular data of weekend temperature and sun information:

https://wolfram.com/xid/0cf2c7rbtteublk-i3i7rr
Pivot the "high" and "low" columns of temperature values:

https://wolfram.com/xid/0cf2c7rbtteublk-od3u0i

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

https://wolfram.com/xid/0cf2c7rbtteublk-h51dn7

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

https://wolfram.com/xid/0cf2c7rbtteublk-zvv1tw

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

https://wolfram.com/xid/0cf2c7rbtteublk-e5s0dz

Applications (2)Sample problems that can be solved with this function
Create a Tabular object with measurements of soil pH at three depth levels of 0, 30 and 80 meters:

https://wolfram.com/xid/0cf2c7rbtteublk-srpc9p

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

https://wolfram.com/xid/0cf2c7rbtteublk-usi1rm


https://wolfram.com/xid/0cf2c7rbtteublk-m4fdfw

Take rectangular data organized by years and months:

https://wolfram.com/xid/0cf2c7rbtteublk-bfsxuv

https://wolfram.com/xid/0cf2c7rbtteublk-k2879o

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

https://wolfram.com/xid/0cf2c7rbtteublk-ykyqet

It is now easy to compute yearly means:

https://wolfram.com/xid/0cf2c7rbtteublk-0qow46

Properties & Relations (3)Properties of the function, and connections to other functions
Take tabular data of weekend temperature and sun information:

https://wolfram.com/xid/0cf2c7rbtteublk-s0a05b
Pivot two sets of columns at the same time:

https://wolfram.com/xid/0cf2c7rbtteublk-hfn551

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

https://wolfram.com/xid/0cf2c7rbtteublk-uhm91z


https://wolfram.com/xid/0cf2c7rbtteublk-gsb2dr

Take tabular data of weekend temperature and sun information:

https://wolfram.com/xid/0cf2c7rbtteublk-bhxjek
Simultaneous pivoting is not equivalent to using the same rules in consecutive separate steps:

https://wolfram.com/xid/0cf2c7rbtteublk-0iksxh


https://wolfram.com/xid/0cf2c7rbtteublk-t8nbuo

Use PivotFromColumns on a Tabular object:

https://wolfram.com/xid/0cf2c7rbtteublk-ddg72y


https://wolfram.com/xid/0cf2c7rbtteublk-w8o166

PivotToColumns is effectively the inverse of PivotFromColumns:

https://wolfram.com/xid/0cf2c7rbtteublk-xfvgpp

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

https://wolfram.com/xid/0cf2c7rbtteublk-227l39


https://wolfram.com/xid/0cf2c7rbtteublk-e9ndrv

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.
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.
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
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: 23-May-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: 23-May-2025
]}