gpt4 book ai didi

python - 将 DataFrame 展平为单行

转载 作者:行者123 更新时间:2023-12-04 23:37:04 25 4
gpt4 key购买 nike

我想重新组织以下多行数据帧,

       1          2       3
A Apple Orange Grape
B Car Truck Plane
C House Apartment Garage

进入这个单行数据帧。
     1_A     2_A    3_A  1_B    2_B    3_B    1_C        2_C     3_C
0 Apple Orange Grape Car Truck Plane House Apartment Garage

感谢您的帮助!

最佳答案

unstack + sort_index救援:

v = df.unstack().to_frame().sort_index(level=1).T
v.columns = v.columns.map('_'.join)
v
1_A 2_A 3_A 1_B 2_B 3_B 1_C 2_C 3_C
0 Apple Orange Grape Car Truck Plane House Apartment Garage

关于python - 将 DataFrame 展平为单行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50556537/

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