gpt4 book ai didi

python - Pandas,将多行转换为多列

转载 作者:行者123 更新时间:2023-12-01 09:20:51 26 4
gpt4 key购买 nike

我有一个示例数据

Index, Column1,Value1
1, 1,6
1, 2,7
1, 3,8
Index, Column1,Value1
2, 11,16
2, 12,17
2, 13,18
2, 14,19
2, 15,20
Index, Column1,Value1
3, 21,24
3, 22,25

我想这样 reshape

Index Column1, Value1, Column2, Value2, Column3, Value3, Column4, Value4
1 1, 6, 2, 7, 3, 8
2 11, 16, 12, 17, 13, 18, 14, 19
3 21, 24, 22, 25

我正在用这个

print(df.groupby('Index')[['Column1', 'Value1']].apply(','.join).reset_index())

但我得到了奇怪的输出。我在这里做错了什么?有什么建议 ?

Index               0
1 Column1,Value1
2 Column1,Value1
3 Column1,Value1
Index Column1,Value1

最佳答案

编辑:

3列输入DataFrame的解决方案:

df = df[df['Column1'] != 'Column1']
s = df.groupby('Index').cumcount().add(1)
df = (df.set_index(['Index',s])
.unstack()
.sort_index(axis=1, level=1)
)
df.columns = ['{}_{}'.format(a, b) for a,b in df.columns]

df = df.reset_index()
print (df)
Index Column1_1 Value1_1 Column1_2 Value1_2 Column1_3 Value1_3 Column1_4 \
0 1 1 6 2 7 3 8 NaN
1 2 11 16 12 17 13 18 14
2 3 21 24 22 25 NaN NaN NaN

Value1_4 Column1_5 Value1_5
0 NaN NaN NaN
1 19 15 20
2 NaN NaN NaN

关于python - Pandas,将多行转换为多列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50793088/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com