Transforms wide-format data into long-format by pivoting specified columns into rows, creating a more normalized data structure for certain types of analysis. Converts horizontal data arrangements into vertical ones.
When to Use
Convert wide data (many columns) to long format (fewer columns, more rows)
Prepare data for time series or longitudinal analysis
Normalize data for visualization tools that require long format
Transform survey data with multiple response columns
Create tidy data for statistical analysis
Melt data for correlation or pattern analysis
Reshape data for hierarchical or faceted visualizations
How It Works
Takes a dataset with multiple related columns (wide format)
Identifies key columns to maintain as identifiers
Pivots specified columns into name-value pairs
Creates a longer dataset with more rows and fewer columns
Maintains data integrity while changing the structure
Updates PDV metadata to reflect the new column structure
Parameters
Required
columns (array) - Columns to pivot into rows
id_cols (array) - Columns to retain as identifiers
Optional
names_to (string) - Name for the new column containing the original column names (default: name)
values_to (string) - Name for the new column containing the values (default: value)
Input Requirements
Dataset must contain the specified columns to pivot
Identifier columns should uniquely identify the non-pivoted dimensions
Transformation Details
Each row in the original dataset generates multiple rows in the result
The number of output rows = (input rows) × (number of pivoted columns)
Identifier columns are repeated for each new row
Original column names become values in the new names_to column
Values from the original columns populate the new values_to column
Output
Data
Longer dataset with original identifier columns plus two new columns
Original columns specified in columns parameter are removed
New column structure allows for easier filtering, grouping, and analysis by column name
PDV
Updated PDV structure reflecting the new column configuration
Metadata for identifier columns is preserved
New metadata added for the names_to and values_to columns
Extras
lengthen_applied - Timestamp when operation was performed
columns_pivoted - List of columns that were pivoted
records_before - Number of records in the original dataset
records_after - Number of records in the lengthened dataset
Example Usage
Basic Column Pivoting
lengthen:
columns: [jan, feb, mar, apr, may, jun]
id_cols: [product_id, category]
names_to: month
values_to: sales