stack: 将列转行。转哪个层级由参数 level 决定,该 level 指的是 columns 所对应的层级
import numpy as np
import pandas as pd
from pandas import Dataframe
index = pd.MultiIndex.from_arrays([['a', 'a', 'b', 'b'], [1, 2, 1, 2]])
columns = pd.MultiIndex.from_product([list("ABC"), ["one", "two"]])
df = DataFrame(np.arange(24).reshape(4, 6), index=index, columns=columns)
In[222]: df
Out[222]:
A B C # columns 的 level 为从上往下依次递增
one two one two one two
a 1 0 1 2 3 4 5
2 6 7 8 9 10 11
b 1 12 13 14 15 16 17
2 18 19 20 21 22 23
In[223]: df.stack(0) # 将列的第一级转到行。
Out[223]:
one two
a 1 A 0 1
B 2 3
C 4 5
2 A 6 7
B 8 9
C 10 11
b 1 A 12 13
B 14 15
C 16 17
2 A 18 19
B 20 21
C 22 23
In[224]: df.stack(1) # 将列的第二级转到行
Out[224]:
A B C
a 1 one 0 2 4
two 1 3 5
2 one 6 8 10
two 7 9 11
b 1 one 12 14 16
two 13 15 17
2 one 18 20 22
two 19 21 23
unstack: 将行转列。转哪个层级由参数 level 决定,该 level 指的是 index 所对应的层级
In[225]: df
Out[225]:
A B C # index 的 level 为从外往内依次递增
one two one two one two
a 1 0 1 2 3 4 5
2 6 7 8 9 10 11
b 1 12 13 14 15 16 17
2 18 19 20 21 22 23
In[226]: df.unstack(0) # 将行的第一级转到列
Out[226]:
A B C
one two one two one two
a b a b a b a b a b a b
1 0 12 1 13 2 14 3 15 4 16 5 17
2 6 18 7 19 8 20 9 21 10 22 11 23
In[227]: df.unstack(1) # 将行的第二级转到列
Out[227]:
A B C
one two one two one two
1 2 1 2 1 2 1 2 1 2 1 2
a 0 6 1 7 2 8 3 9 4 10 5 11
b 12 18 13 19 14 20 15 21 16 22 17 23