使用pivot
:
print (df)
A B C
0 a x 10
1 b x 11
2 c x 15
3 a y 16
4 b y 17
5 c y 19
6 a z 20
7 b z 21
8 c z 23
df = df.pivot(index='A', columns='B', values='C')
print (df)
B x y z
A
a 10 16 20
b 11 17 21
c 15 19 23
或者set_index
+ unstack
:
df = df.set_index(['A','B'])['C'].unstack()
print (df)
B x y z
A
a 10 16 20
b 11 17 21
c 15 19 23
如果重複使用pivot_table
與像mean
,sum
聚合函數...:
print (df)
A B C
0 a x 10 <-same a,x different C = 10
1 a x 13 <-same a,x different C = 13
2 b x 11
3 c x 15
4 a y 16
5 b y 17
6 c y 19
7 a z 20
8 b z 21
9 c z 23
df = df.pivot_table(index='A', columns='B', values='C', aggfunc='mean')
或者groupby
+ aggregate function
+ set_index
:
df = df.groupby(['A','B'])['C'].mean().unstack()
print (df)
B x y z
A
a 11.5 16.0 20.0 <- (10 + 13)/2 = 11.5
b 11.0 17.0 21.0
c 15.0 19.0 23.0