# Unpivot

`Unpivot` is a powerful tool to convert table columns into rows. Specifically, it transforms multiple columns into two columns: `unpivot_table_metric` and `unpitvot_table_value` . Column names are placed in the former columns and their corresponding values are in the latter column.  Notice that `Unpivot` is not the exact reverse of the [Pivot Table](https://docs.acho.io/acho-studio/data-prep-projects/applying-actions/..#16-pivot-table) action, since it doesn't do any aggregation or produce a summary table. That is, `Unpivot` doesn't merge several values into one value. It merely changes the format of a table by transforming a wide table into a long table.<br>

Below is an example table that shows how often people wear masks in two counties of New York during COVID-19. `Never`, `rarely`, `sometimes`, `frequently`, and `always` refers to the proportion of the population in different levels of mask-wearing frequency. We can combine them together by applying the `Unpivot` action, so the table becomes a longer table with fewer columns (from 7 columns to 4 columns).

![Unpivot example - frequency of mask-wearing](https://3574406564-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MB_fx7PCUqvFEdrucJC%2F-MZnUiPsWU_eWOlicK_Y%2F-MZnYYdrqtdCAvkJPwUa%2Fimage.png?alt=media\&token=7a785b35-1b87-4728-b8bc-7ab5370afe29)

## How to apply Unpivot?

There are two options under `Unpivot` in Acho:

* **Columns to unpivot**

  You can click the dropdown menu to choose one or more columns to unpivot.

![Unpivot Panel - Choosing columns to unpivot](https://3574406564-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MB_fx7PCUqvFEdrucJC%2F-MZnUiPsWU_eWOlicK_Y%2F-MZnZjBePg0ukpw4lZVE%2Fimage-20210430162049454.png?alt=media\&token=42290fcf-04a9-493e-bf73-c7f514bfda97)

* **Columns to keep**

  For 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](https://3574406564-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MB_fx7PCUqvFEdrucJC%2F-MZnUiPsWU_eWOlicK_Y%2F-MZnZqzeugPY4hS6FhIT%2Fimage-20210430162038304.png?alt=media\&token=123d10c0-2687-4e0f-b9ce-57649d5f3a1f)

## **Data types for unpivoted columns**

1. If unpivoted columns are all in the same type, the `unpivot_table_value`column 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_value`column will result in a string type. Below is an example to unpivot columns that have mixing types(integer, string, and date).&#x20;

![](https://3574406564-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MB_fx7PCUqvFEdrucJC%2F-MZnuk_NFJ9cRE_dOzAm%2F-MZnyFLtourOF4ozTE6t%2Fimage.png?alt=media\&token=f9b26b10-7a3a-4ab3-9f12-2b191e66c7c1)

## **When to use Unpivot?**

1. When you have multiple columns that share the same characteristics (for example, `never`, `rarely`, `sometimes`, `frequently`, and `always` refers 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.](https://3574406564-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MB_fx7PCUqvFEdrucJC%2F-MZnUiPsWU_eWOlicK_Y%2F-MZn_6Y6PPTFz3cHbWOD%2Fimage-20210430165016898.png?alt=media\&token=9385fdfc-4e0b-4103-9a8c-f74713f1e266)
