Suppose we have a stacked data i.e. column is stacked row wise. Now we wish to unstack it. It can easily be done by unstack() function.
So this recipe is a short example on What is unstacking and squeeze in pandas dataframe. Let's get started.
import pandas as pd import seaborn as sb
Let's pause and look at these imports. Pandas is generally used for performing mathematical operation and preferably over arrays. Seaborn is just used in here to import dataset.
df = sb.load_dataset('tips') stacked_df=df.stack() print(stacked_df)
Here we have imported tips dataset from seaborn library. Now, to produce a stacked dataframe, we are using stack function to modify the current.
Now our dataset is ready.
Here we unstacked our datafram using unstack() function.
Once we run the above code snippet, we will see:
Scroll down to the ipython file to look at the results.
We can see data returning to original format. Similarity for multiindex format, we will have same results.