pandas.DataFrame.stack

pandas 3.1.1 · DataFrame · method

Move column levels into the row index.

Call syntax

DataFrame.stack(level=-1, dropna=…, sort=…, future_stack=True)

Run the example

Edit the example and select Run Python to see the result.

Parameters

ParameterRequirementAccepted values and behavior
level Optional
Positional or keyword
Default -1
int, str, list

Choose an index level by name or integer position.

dropna Optional
Positional or keyword
bool

Choose whether to drop missing values or missing keys from consideration.

sort Optional
Positional or keyword
bool

Choose whether the resulting labels or groups are sorted where supported.

future_stack Optional
Positional or keyword
Default True
bool

Returns

DataFrame or Series