October 2019
Beginner to intermediate
674 pages
16h 43m
English
The unstack function performs the reverse operation of the stack function. It converts long DataFrames to a wider format. Let's unstack the multi-level indexed sales data. The last level is unstacked by default:
multi_df.unstack()
The following will be the output:

Just like stack, unstack has a level parameter. This level parameter accepts a level number, a level name, or a list of level names/level numbers.
Any missing values created when unstacking can be handled using the fill_value argument of the unstack function. Consider the following DataFrame:
multi_df.iloc[[0,5,6],[0,2]]
The following will be the ...
Read now
Unlock full access