Unpivotis a powerful tool to convert table columns into rows. Specifically, it transforms multiple columns into two columns:
unpitvot_table_value. Column names are placed in the former columns and their corresponding values are in the latter column. Notice that
Unpivotis not the exact reverse of the Pivot Table action, since it doesn't do any aggregation or produce a summary table. That is,
Unpivotdoesn't merge several values into one value. It merely changes the format of a table by transforming a wide table into a long table.
Below is an example table that shows how often people wear masks in two counties of New York during COVID-19.
alwaysrefers to the proportion of the population in different levels of mask-wearing frequency. We can combine them together by applying the
Unpivotaction, so the table becomes a longer table with fewer columns (from 7 columns to 4 columns).
Unpivot example - frequency of mask-wearing
There are two options under
- Columns to unpivotYou can click the dropdown menu to choose one or more columns to unpivot.
Unpivot Panel - Choosing columns to unpivot
- Columns to keepFor the rest of the columns that are not used for unpivoting, you can decide whether to keep them or not. This option is optional. You can specify 0 or more columns to keep.
Unpivot Panel - Choosing columns to keep
- 1.If unpivoted columns are all in the same type, the
unpivot_table_valuecolumn will have the same data type. Take the mask-wearing data as an example. Since all input columns are in a float type, the type of the resulting column is also Float.
- 2.If unpivoted columns have mixing types (such as integer and string), the
unpivot_table_valuecolumn will result in a string type. Below is an example to unpivot columns that have mixing types(integer, string, and date).
- 1.When you have multiple columns that share the same characteristics (for example,
alwaysrefers to the frequency of mask-wearing), you want to combine them together.
- 2.When you have multiple columns that indicate distinct metrics (such as the number of confirmed cases and deaths of COVID-19), you want to create a line chart or column chart to compare these metrics at the same time.
Using Unpivot to create a split-line chart.