If you want to pivot the data, rather than unpivot, use the Aggregate gem.
Parameters
| Parameter | Description |
|---|---|
| Column(s) to use as identifiers | The column(s) that will identify to which group or entity the observation corresponds to. |
| Columns to unpivot | The columns (wide format) that you would like to transform into a single column (long format). |
| Variable column name | The name of the column that contains the names of the unpivoted columns. This helps describe the values in the value column. |
| Value column name | The name of the column that will contain the values from the unpivoted columns. |
Example
Imagine you have sales data for different products, with each quarter’s sales stored in its own column—this is known as wide format. Before modeling seasonal trends or doing time series analysis, it’s often helpful to convert this into long format, where each row represents a single observation.| Product | Q1 | Q2 | Q3 | Q4 |
|---|---|---|---|---|
| A | 100 | 150 | 130 | 170 |
| B | 90 | 120 | 110 | 160 |
- Select the identifier columns. In the example above, the
Productcolumn is the identifier column. - Select the columns to unpivot. In the example above, all of the quarter columns (
Q1,Q2, etc.) are your columns to unpivot. - Name the variable column
Quarterbecause it identifies the sales period. - Name the value column
UnitsSoldbecause it contains number of units sold per quarter. - Save and run the gem.
- The quarter names (
Q1,Q2, etc.) will move into a newQuartercolumn. - The corresponding sales values will be stored in a
UnitsSoldcolumn.
| Product | Quarter | UnitsSold |
|---|---|---|
| A | Q1 | 100 |
| A | Q2 | 150 |
| A | Q3 | 130 |
| A | Q4 | 170 |
| B | Q1 | 90 |
| B | Q2 | 120 |
| B | Q3 | 110 |
| B | Q4 | 160 |

