我有以下格式的数据框:
DF1:原始数据框的形状为(1200,9)
这是示例数据框
x1 x2 x3 Group
1.0 0.0 0.0 A
0.0 0.0 0.0 A
0.0 3.0 11.0 A
0.0 0.0 0.0 A
0.0 1.0 0.0 A
0.0 0.0 0.0 E
0.0 0.0 0.0 E
0.0 0.0 0.0 E
0.0 0.0 6.0 E
0.0 0.0 0.0 E
我想要以下格式的输出:
DF_Res:
Group A E
x1 1.0 0.0
x2 0.0 0.0
x3 0.0 0.0
x1 0.0 0.0
x2 0.0 0.0
x3 0.0 0.0
x1 0.0 0.0
x2 3.0 0.0
x3 11.0 0.0
x1 0.0 0.0
x2 0.0 0.0
x3 0.0 6.0
x1 0.0 0.0
x2 1.0 0.0
x3 0.0 0.0
我想对列进行转换,以使它们成为组,而组成为列标题。
请帮助。
谢谢
答案 0 :(得分:4)
如果要汇总值,例如每组sum
个
df1 = df.groupby('Group').sum().T.rename_axis(None, axis=1).rename_axis('Group').reset_index()
print (df1)
Group A E
0 x1 0.0 0.0
1 x2 0.0 0.0
2 x3 0.0 0.0
3 x4 0.0 0.0
4 x5 0.0 0.0
5 x6 0.0 0.0
6 x7 0.0 0.0
7 x8 0.0 0.0
编辑:
df2 = df.set_index('Group').T.rename_axis(None, axis=1).rename_axis('Group').reset_index()
print (df2)
Group A A E E A
0 x1 0.0 0.0 0.0 0.0 0.0
1 x2 0.0 0.0 0.0 0.0 0.0
2 x3 0.0 0.0 0.0 0.0 0.0
3 x4 0.0 0.0 0.0 0.0 0.0
4 x5 0.0 0.0 0.0 0.0 0.0
5 x6 0.0 0.0 0.0 0.0 0.0
6 x7 0.0 0.0 0.0 0.0 0.0
7 x8 0.0 0.0 0.0 0.0 0.0
EDIT1:
df = (df.set_index('Group')
.groupby(level=0)
.apply(lambda x: x.stack().reset_index(level=0, drop=True))
.rename_axis(None)
.rename_axis('Group', axis=1)
.T
.reset_index())
print (df)
Group A E
0 x1 1.0 0.0
1 x2 0.0 0.0
2 x3 0.0 0.0
3 x1 0.0 0.0
4 x2 0.0 0.0
5 x3 0.0 0.0
6 x1 0.0 0.0
7 x2 3.0 0.0
8 x3 11.0 0.0
9 x1 0.0 0.0
10 x2 0.0 0.0
11 x3 0.0 6.0
12 x1 0.0 0.0
13 x2 1.0 0.0
14 x3 0.0 0.0
答案 1 :(得分:1)
这有点“棘手”,但是您需要创建一个单独的索引来区分您的值。例如,多个值对应于A
和x1
。这就是我在说的:
df_new = df.set_index('Group')
df_new = df_new.groupby(df_new.index, as_index=False).apply(lambda x: x.stack().reset_index())
df_new.columns = ['Group', 'x', 'value']
df_new = df_new.droplevel(axis=0, level=0).set_index(['Group', 'x'], append=True).unstack('Group').droplevel(axis=1, level=0)
结果:
Group A E
x
x1 1.0 0.0
x2 0.0 0.0
x3 0.0 0.0
x1 0.0 0.0
x2 0.0 0.0
x3 0.0 0.0
x1 0.0 0.0
x2 3.0 0.0
x3 11.0 0.0
x1 0.0 0.0
x2 0.0 0.0
x3 0.0 6.0
x1 0.0 0.0
x2 1.0 0.0
x3 0.0 0.0