pandas.DataFrame.unstack

DataFrame.unstack(level=-1)

Pivot a level of the (necessarily hierarchical) index labels, returning a DataFrame having a new level of column labels whose inner-most level consists of the pivoted index labels. If the index is not a MultiIndex, the output will be a Series (the analogue of stack when the columns are not a MultiIndex)

Parameters :

level : int, string, or list of these, default last level

Level(s) of index to unstack, can pass level name

Returns :

unstacked : DataFrame or Series

Examples

>>> s
one  a   1.
one  b   2.
two  a   3.
two  b   4.
>>> s.unstack(level=-1)
     a   b
one  1.  2.
two  3.  4.
>>> df = s.unstack(level=0)
>>> df
   one  two
a  1.   2.
b  3.   4.
>>> df.unstack()
one  a  1.
     b  3.
two  a  2.
     b  4.