gpt4 book ai didi

python - 合并 Pandas 中两行的内容

转载 作者:行者123 更新时间:2023-11-28 21:33:43 25 4
gpt4 key购买 nike

我有一个数据框,我想合并两行的内容,并在同一个单元格中用下划线分隔。如果这是原始 DF:

0   eye-right   eye-right   hand
1 location location position
2 12 27.7 2
3 14 27.6 2.2

我希望它变成:

0   eye-right_location   eye-right_location   hand_position
1 12 27.7 2
2 14 27.6 2.2

最终我想将第 0 行翻译成标题,并为整个 df 重置索引。

最佳答案

您可以设置您的列标签,通过iloc切片,然后reset_index:

print(df)
# 0 1 2
# 0 eye-right eye-right hand
# 1 location location position
# 2 12 27.7 2
# 3 14 27.6 2.2

df.columns = (df.iloc[0] + '_' + df.iloc[1])
df = df.iloc[2:].reset_index(drop=True)

print(df)
# eye-right_location eye-right_location hand_position
# 0 12 27.7 2
# 1 14 27.6 2.2

关于python - 合并 Pandas 中两行的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54311068/

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