Pandas 合并具有不同行名称的行
Pandas combining rows with different row Names
我关注pandas table:
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
我想安排这个table:
A1 A2 A3 A4
0.202425 0.13495 0.202425 0.202425
0.94465 0.877175 0.877175 0.8097
您可以select将columns
的两组,重命名第二组以匹配第一组,然后使用pd.concat()
垂直组合:
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 table:
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
我想安排这个table:
A1 A2 A3 A4
0.202425 0.13495 0.202425 0.202425
0.94465 0.877175 0.877175 0.8097
您可以select将columns
的两组,重命名第二组以匹配第一组,然后使用pd.concat()
垂直组合:
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