Pandas 将行与不同的行名称组合在一起 [英] Pandas combining rows with different row Names
本文介绍了Pandas 将行与不同的行名称组合在一起的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下熊猫表:
A1 A2 A3 A4 B1 B2 B3 B4
3 0.202425 0.13495 0.202425 0.202425 0.94465 0.877175 0.877175 0.8097
我想安排这张桌子:
A1 A2 A3 A4
0.202425 0.13495 0.202425 0.202425
0.94465 0.877175 0.877175 0.8097
推荐答案
您可以选择两组columns
,重命名第二组以匹配第一组,然后使用pd.concat()
垂直组合:
You could select the two groups of columns
, rename the second group to match the first group, and then use pd.concat()
to combine vertically:
a_cols = [c for c in df.columns if c.startswith('A')]
b_cols = [c for c in df.columns if c not in a_cols]
col_dict = dict(zip(b_cols, a_cols))
pd.concat([df.loc[:, a_cols], df.loc[:, b_cols].rename(columns=col_dict)])
A1 A2 A3 A4
0 0.202425 0.134950 0.202425 0.202425
0 0.944650 0.877175 0.877175 0.809700
这篇关于Pandas 将行与不同的行名称组合在一起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文