gpt4 book ai didi

python - Pandas 重新排序数据

转载 作者:行者123 更新时间:2023-12-01 03:39:39 25 4
gpt4 key购买 nike

使用数据透视表这可能是一个简单的方法,但由于我没有添加数字(每一行都是唯一的),我应该如何去做呢?

输入:

      Col1   Col2 Col3
0 123.0 33.0 ABC
1 345.0 39.0 ABC
2 567.0 100.0 ABC
3 123.0 82.0 PQR
4 345.0 10.0 PQR
5 789.0 38.0 PQR
6 890.0 97.0 XYZ
7 345.0 96.0 XYZ

输出:

      Col1    ABC   PQR   XYZ
0 123.0 33.0 82.0 NaN
1 345.0 39.0 10.0 96.0
2 567.0 100.0 NaN NaN
3 789.0 NaN 38.0 NaN
4 890.0 NaN NaN 97.0

我可以得到数据帧格式的输出吗?非常感谢您的浏览!

最佳答案

您可以使用pivot :

print (df.pivot(index='Col1', columns='Col3', values='Col2'))
Col3 ABC PQR XYZ
Col1
123.0 33.0 82.0 NaN
345.0 39.0 10.0 96.0
567.0 100.0 NaN NaN
789.0 NaN 38.0 NaN
890.0 NaN NaN 97.0

另一个解决方案 set_indexunstack :

print (df.set_index(['Col1','Col3'])['Col2'].unstack())
Col3 ABC PQR XYZ
Col1
123.0 33.0 82.0 NaN
345.0 39.0 10.0 96.0
567.0 100.0 NaN NaN
789.0 NaN 38.0 NaN
890.0 NaN NaN 97.0
<小时/>

按评论编辑:

需要pivot_table :

print (df.pivot_table(index='Col1', columns='Col3', values='Col2'))
Col3 ABC PQR XYZ
Col1
123.0 33.0 82.0 NaN
345.0 39.0 10.0 96.0
567.0 100.0 NaN NaN
789.0 NaN 38.0 NaN
890.0 NaN NaN 97.0

另一个更快的解决方案 groupby ,聚合 mean(默认情况下 pivot_table 也聚合 mean),通过 DataFrame.squeeze 转换为 Series最后unstack :

print (df.groupby(['Col1','Col3']).mean().squeeze().unstack())
Col3 ABC PQR XYZ
Col1
123.0 33.0 82.0 NaN
345.0 39.0 10.0 96.0
567.0 100.0 NaN NaN
789.0 NaN 38.0 NaN
890.0 NaN NaN 97.0

关于python - Pandas 重新排序数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39825550/

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