In [1]:
import pandas as pd
import numpy as np
from pandas import Series, DataFrame
In [2]:
dframe1 = DataFrame(np.arange(8).reshape(2,4), index = pd.Index(['LA', 'SF'], name='city'), columns = pd.Index(list('ABCD'), name='letter'))
In [3]:
dframe_st = dframe1.stack()
dframe_st
Out[3]:
In [4]:
dframe_st.unstack()
Out[4]:
In [5]:
dframe_st.unstack('city')
Out[5]:
In [6]:
ser1 = Series([0,1,2], index=list('QXY'))
ser2 = Series([4,5,6], index=list('XYZ'))
In [7]:
dframe = pd.concat([ser1, ser2], keys=['Alpha', 'Beta'])
dframe
Out[7]:
In [8]:
dframe.unstack()
Out[8]:
In [9]:
dframe.unstack().stack()
Out[9]:
In [10]:
dframe.unstack().stack(dropna=False)
Out[10]:
In [ ]: