October 2019
Beginner to intermediate
674 pages
16h 43m
English
When stacking, a set of column labels get converted to an index level. To explore stacking further, let's use a DataFrame with a MultiIndex along the row-index and column-index:
multi_df = sales_data[["Sales", "Quantity", "Category", "ShipMode"]].groupby(["Category", "ShipMode"]).agg([np.sum, np.mean])multi_df
The following will be the output:

Applying stack() makes a wide DataFrame longer. Let's apply stack() on the preceding DataFrame. The column labels on the last level get added to the MultiIndex:
multi_df.stack()
The following will be the output:
Read now
Unlock full access