重塑和透视
使用多层索引进行重塑
statck
(堆叠)该操作会“旋转”或将列中的数据透视到行。unstack
(拆堆)该操作会将行中的数据透视到列。
In [5]: data = pd.DataFrame(np.arange(6).reshape((2,3)), index=pd.Index(['Ohio', 'Colora
...: do'], name='state'), columns=pd.Index(['one', 'two', 'three'], name='number'))
In [6]: data
Out[6]:
number one two three
state
Ohio 0 1 2
Colorado 3 4 5
# 使用stack方法会将列透视到行,产生一个新的Series:
In [7]: result = data.stack()
In [8]: result
Out