转置或枢转Pandas中的多列

当中

我想在一个数据框中转置多列。我浏览了大多数的转置和枢纽熊猫文章,但无法使其正常工作。

这是我的数据框的外观。

df = pd.DataFrame()
df['L0'] = ['fruit', 'fruit', 'fruit', 'fruit', 'fruit', 'fruit', 'vegetable', 'vegetable', 'vegetable', 'vegetable', 'vegetable', 'vegetable']
df['L1'] = ['apple', 'apple', 'apple', 'banana', 'banana', 'banana', 'tomato', 'tomato', 'tomato', 'lettuce', 'lettuce', 'lettuce']
df['Type'] = ['X', 'Y', 'Z', 'X', 'Y', 'Z', 'X', 'Y', 'Z', 'X', 'Y', 'Z']
df['A'] = [3, 0, 4, 3, 1, 3, 2, 2, 2, 4, 2, 4]
df['B'] = [3, 1, 0, 4, 1, 4, 4, 4, 2, 1, 2, 1]
df['C'] = [0, 4, 1, 0, 2, 4, 1, 1, 2, 3, 2, 3]

我想转置/旋转A,B和C列,并将其替换为“ Type”列中的值。结果数据框应如下所示。

df2 = pd.DataFrame()
df2['L0'] = ['fruit', 'fruit', 'fruit', 'fruit', 'fruit', 'fruit', 'vegetable', 'vegetable', 'vegetable', 'vegetable', 'vegetable', 'vegetable']
df2['L1'] = ['apple', 'apple', 'apple', 'banana', 'banana', 'banana', 'tomato', 'tomato', 'tomato', 'lettuce', 'lettuce', 'lettuce']
df2['Type2'] = ['A', 'B', 'C', 'A', 'B', 'C', 'A', 'B', 'C', 'A', 'B', 'C']
df2['X'] = [3, 3, 0, 3, 4, 0, 2, 4, 1, 4, 1, 3]
df2['Y'] = [0, 1, 4, 1, 1, 2, 2, 4, 1, 2, 2, 2]
df2['Z'] = [4, 0, 1, 3, 4, 4, 2, 2, 2, 4, 1, 3]

我能做的最好的就是

df.groupby(['L0', 'L1', 'Type'])['A', 'B', 'C'].sum().unstack('Type')

但这不是我真正想要的。谢谢!

耶斯列尔

stack在之前添加unstack

df = (df.groupby(['L0', 'L1', 'Type'])['A', 'B', 'C']
        .sum()
        .stack()
        .unstack('Type')
        .reset_index()
        .rename_axis(None, axis=1)
        .rename(columns={'level_2':'Type2'}))
print (df)
           L0       L1 Type2  X  Y  Z
0       fruit    apple     A  3  0  4
1       fruit    apple     B  3  1  0
2       fruit    apple     C  0  4  1
3       fruit   banana     A  3  1  3
4       fruit   banana     B  4  1  4
5       fruit   banana     C  0  2  4
6   vegetable  lettuce     A  4  2  4
7   vegetable  lettuce     B  1  2  1
8   vegetable  lettuce     C  3  2  3
9   vegetable   tomato     A  2  2  2
10  vegetable   tomato     B  4  4  2
11  vegetable   tomato     C  1  1  2

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章