- API reference
- DataFrame
- pandas.DataF...
pandas.DataFrame.stack#
- DataFrame.stack(level=-1,dropna=<no_default>,sort=<no_default>,future_stack=False)[source]#
Stack the prescribed level(s) from columns to index.
Return a reshaped DataFrame or Series having a multi-levelindex with one or more new inner-most levels compared to the currentDataFrame. The new inner-most levels are created by pivoting thecolumns of the current dataframe:
if the columns have a single level, the output is a Series;
if the columns have multiple levels, the new indexlevel(s) is (are) taken from the prescribed level(s) andthe output is a DataFrame.
- Parameters:
- levelint, str, list, default -1
Level(s) to stack from the column axis onto the indexaxis, defined as one index or label, or a list of indicesor labels.
- dropnabool, default True
Whether to drop rows in the resulting Frame/Series withmissing values. Stacking a column level onto the indexaxis can create combinations of index and column valuesthat are missing from the original dataframe. See Examplessection.
- sortbool, default True
Whether to sort the levels of the resulting MultiIndex.
- future_stackbool, default False
Whether to use the new implementation that will replace the currentimplementation in pandas 3.0. When True, dropna and sort have no impacton the result and must remain unspecified. Seepandas 2.1.0 Releasenotes for more details.
- Returns:
- DataFrame or Series
Stacked dataframe or series.
See also
DataFrame.unstack
Unstack prescribed level(s) from index axis onto column axis.
DataFrame.pivot
Reshape dataframe from long format to wide format.
DataFrame.pivot_table
Create a spreadsheet-style pivot table as a DataFrame.
Notes
The function is named by analogy with a collection of booksbeing reorganized from being side by side on a horizontalposition (the columns of the dataframe) to being stackedvertically on top of each other (in the index of thedataframe).
Referencethe user guide for more examples.
Examples
Single level columns
>>>df_single_level_cols=pd.DataFrame([[0,1],[2,3]],...index=['cat','dog'],...columns=['weight','height'])
Stacking a dataframe with a single level column axis returns a Series:
>>>df_single_level_cols weight heightcat 0 1dog 2 3>>>df_single_level_cols.stack(future_stack=True)cat weight 0 height 1dog weight 2 height 3dtype: int64
Multi level columns: simple case
>>>multicol1=pd.MultiIndex.from_tuples([('weight','kg'),...('weight','pounds')])>>>df_multi_level_cols1=pd.DataFrame([[1,2],[2,4]],...index=['cat','dog'],...columns=multicol1)
Stacking a dataframe with a multi-level column axis:
>>>df_multi_level_cols1 weight kg poundscat 1 2dog 2 4>>>df_multi_level_cols1.stack(future_stack=True) weightcat kg 1 pounds 2dog kg 2 pounds 4
Missing values
>>>multicol2=pd.MultiIndex.from_tuples([('weight','kg'),...('height','m')])>>>df_multi_level_cols2=pd.DataFrame([[1.0,2.0],[3.0,4.0]],...index=['cat','dog'],...columns=multicol2)
It is common to have missing values when stacking a dataframewith multi-level columns, as the stacked dataframe typicallyhas more values than the original dataframe. Missing valuesare filled with NaNs:
>>>df_multi_level_cols2 weight height kg mcat 1.0 2.0dog 3.0 4.0>>>df_multi_level_cols2.stack(future_stack=True) weight heightcat kg 1.0 NaN m NaN 2.0dog kg 3.0 NaN m NaN 4.0
Prescribing the level(s) to be stacked
The first parameter controls which level or levels are stacked:
>>>df_multi_level_cols2.stack(0,future_stack=True) kg mcat weight 1.0 NaN height NaN 2.0dog weight 3.0 NaN height NaN 4.0>>>df_multi_level_cols2.stack([0,1],future_stack=True)cat weight kg 1.0 height m 2.0dog weight kg 3.0 height m 4.0dtype: float64