假设我们有这样的数据框:
df = pd.DataFrame({'key' : ['one', 'two', 'three', 'four'] * 3,
'col' : ['A', 'B', 'C'] * 4,
'val1' : np.random.randn(12),
'val2' : np.random.randn(12),
'val3' : np.random.randn(12)})
key + col
是唯一键
我想使col
值成为列拆分或对其进行交叉制表,最后看起来像这样:
第一个幼稚的方法pd.crosstab(df.key,df.col)
在这里效果不佳:
此代码pd.crosstab(df.key,df.col,values = df[['val1', 'val2', 'val3']], aggfunc = np.max)
无法与ValueError: Wrong number of items passed 3, placement implies 1
一起运行
它如何工作?
答案 0 :(得分:5)
将pivot_table
与swaplevel
和sort_index
与聚合函数np.max
一起使用:
df = (df.pivot_table(index='key', columns='col', aggfunc=np.max)
.swaplevel(0,1,axis=1)
.sort_index(axis=1))
替代品由GroupBy.max
汇总:
df = (df.groupby(['key', 'col'])
.max()
.unstack()
.swaplevel(0,1,axis=1)
.sort_index(axis=1))
print (df)
col A B C \
val1 val2 val3 val1 val2 val3 val1
key
four -0.225967 0.362041 0.040915 -1.227718 -0.879248 -1.279912 -1.577218
one -0.187167 1.530731 -1.112116 -0.871077 -2.099876 -0.069297 -0.351971
three -0.165375 -0.378049 -0.390724 0.484519 -0.408990 -1.496042 0.590083
two 1.923084 -0.688284 1.702659 -0.159921 0.635245 0.623821 -1.503893
col
val2 val3
key
four -1.135872 0.645371
one 2.347472 0.129252
three 0.402825 0.883710
two -0.132847 0.179476
答案 1 :(得分:4)
使用melt
,set_index
和unstack
,这仅在您希望每个像元的值时才有效,否则,您可以使用第二个选项来聚合值:
df.melt(['key','col'])\
.set_index(['key','col','variable'])['value']\
.unstack([1,2])\
.sort_index(axis=1)
输出:
col A B C
variable val1 val2 val3 val1 val2 val3 val1 val2 val3
key
four -1.964246 0.958854 -0.605128 0.055120 -1.144306 -0.800712 -0.917324 -0.581882 -0.152399
one 0.513347 -1.689448 -2.434481 0.990924 -1.014848 0.713703 1.344299 0.052877 1.174183
three -0.156336 -0.156157 -2.253689 0.877726 -0.686758 -0.407892 0.816636 1.008870 -0.390872
two 1.942495 1.811712 -0.762283 -2.169613 -1.073372 0.201996 -1.073370 -0.902032 -0.168796
使用melt
和pd.crosstab
的另一个选项:
df1 = df.melt(['key','col'])
pd.crosstab(df1.key, [df1.col, df1.variable], df1.value, aggfunc=np.max)
输出:
col A B C
variable val1 val2 val3 val1 val2 val3 val1 val2 val3
key
four -1.964246 0.958854 -0.605128 0.055120 -1.144306 -0.800712 -0.917324 -0.581882 -0.152399
one 0.513347 -1.689448 -2.434481 0.990924 -1.014848 0.713703 1.344299 0.052877 1.174183
three -0.156336 -0.156157 -2.253689 0.877726 -0.686758 -0.407892 0.816636 1.008870 -0.390872
two 1.942495 1.811712 -0.762283 -2.169613 -1.073372 0.201996 -1.073370 -0.902032 -0.168796