Long-form reshaping
Convert wide, single-level columns into a labeled long-form Series for iteration or downstream grouping.
Pandas · Module
Reshapes a DataFrame by moving selected column levels into the index. The result is a Series for single-level columns or a DataFrame when remaining column levels exist.
DataFrame.stack reshapes columns into one or more innermost index levels. With ordinary single-level columns it returns a Series; with selected levels from MultiIndex columns it can return a DataFrame. Missing-value removal and level ordering are controlled by the optional arguments.
DataFrame.stack(*level=-1*, *dropna=<no_default>*, *sort=<no_default>*, *future_stack=True*)| Name | Type | Description |
|---|---|---|
level | int, str, or list; default -1 | Column level or levels to move to the index; an integer, label, or list of integers or labels. Default: -1. |
dropna | bool; default True | Whether to remove rows containing missing values from the result. Default: True. |
sort | bool; default True | Whether to sort the resulting MultiIndex levels. Default: True. |
future_stack | bool; default True | Whether to use the newer stacking implementation intended to replace the current one in pandas 3.0. Default: True in the current stable documentation. |
DataFrame or Series — the stacked dataframe or series.
import pandas as pd
df = pd.DataFrame(
{"name": ["Alice", "Bob"], "score": [90, 85]},
index=["exam1", "exam2"],
)
print(df.stack())This converts the single-level columns into an inner index level, producing a Series with row labels and column names in its MultiIndex.
Convert wide, single-level columns into a labeled long-form Series for iteration or downstream grouping.
Move selected levels of hierarchical columns into the row index while retaining other column levels.
Preserve or remove missing-valued combinations during reshaping according to downstream data requirements.
stack moves the selected column level or levels onto the index and creates new innermost MultiIndex levels. If the columns have one level, the result is a Series; with multiple column levels, the result can be a DataFrame.
Follow-up: What happens when columns are a MultiIndex and only one level is stacked? The chosen level becomes an innermost index level, while the unstacked column levels remain columns, so the result stays a DataFrame.
level identifies the column level or levels to move, using an integer position, a label, or a list of either. Its default is -1, which selects the last column level.
Follow-up: How would you stack two named levels? Pass their labels as a list, for example level=["region", "metric"], provided those levels exist in the column index.
dropna controls whether rows containing missing values are removed from the reshaped result, while sort controls ordering of the resulting MultiIndex levels. Both options are configurable rather than requiring a separate cleanup step.
Follow-up: When should future_stack be considered? Use it to opt into the newer implementation that is intended to replace the current one in pandas 3.0, while checking compatibility with the surrounding code.
DataFrame.unstackDataFrame.meltDataFrame.pivotDataFrame.droplevel