Normalize time-suffixed measurements
Transform columns such as sales_2023 and sales_2024 into one sales column indexed by year.
Pandas · Function
Unpivot a DataFrame whose measured columns share stub prefixes and suffixes. Identifier columns become part of the resulting index, while the extracted suffix is named by j.
Provide the wide DataFrame, stub names for column groups, identifier columns, and the name for the extracted suffix. Matching columns such as A_2023 and A_2024 are reshaped into an A column indexed by the identifiers and the suffix; other columns remain unchanged.
pandas.wide_to_long(df, stubnames, i, j, sep='', suffix='\d+')| Name | Type | Description |
|---|---|---|
df | DataFrame | The wide-format DataFrame. |
stubnames | str or list-like | The stub name or names; wide variables are assumed to start with them. |
i | str or list-like | Column or columns used as identifier variables. |
j | str | Name of the sub-observation variable in the long format. |
sep | str, default '' | Separator in wide column names to strip from long-format names; default is an empty string. |
suffix | str, default '\d+' | Regular expression capturing the desired suffixes; default is '\d+'. |
DataFrame containing each stub name as a variable, with a new index formed from (i, j).
import pandas as pd
df = pd.DataFrame({
"id": [1, 2],
"A_2023": [10, 20],
"A_2024": [11, 21],
})
long = pd.wide_to_long(df, stubnames="A", i="id", j="year", sep="_")
print(long.reset_index())The identifier column remains an index level, while matching A_year columns become one A value column and a year sub-observation column.
Transform columns such as sales_2023 and sales_2024 into one sales column indexed by year.
Reshape several parallel groups such as A_2023 and B_2023 together.
Preserve identifiers and unrelated metadata while converting repeated measurements to rows.
It reshapes columns grouped by a shared stub and suffix into a long DataFrame. The supplied i columns identify rows, while j names the extracted suffix level.
Follow-up: How would you handle columns such as A-2023? Pass sep="-" so the separator is removed when forming the long-format names.
stubnames identifies the prefixes of measured-column groups, i supplies identifier columns, and j names the sub-observation variable. For example, stubnames="A" matches A1 and A2 with the default separator and suffix rules.
Follow-up: How can multiple measured groups be reshaped together? Supply a list-like stubnames value, such as ["A", "B"].
sep specifies the separator between a stub and suffix and is stripped from long-format names. suffix is a regular expression for accepted suffixes; the default \d+ selects numeric suffixes.
Follow-up: What pattern selects nonnumeric suffixes? Use a negated digit class such as \D+ when the suffixes contain no numbers.
pandas.meltDataFrame.meltpandas.pivot