【发布时间】:2021-09-01 07:32:14
【问题描述】:
我有两个数据框,我可以合并它。但我想以特定格式(按列)合并它,下面是更多详细信息
>df1
id A B C
0 1 20 0 1
1 2 23 1 2
>df2
id A B C
0 1 10 1 1
1 2 20 1 1
下面是我的代码和输出
df = pd.merge(df1,df2,on='id',suffixes=('_Pre', '_Post'))
这个输出是:
id A_Pre B_Pre C_Pre A_Post B_Post C_Post
0 1 20 0 1 10 1 1
1 2 23 1 2 20 1 1
但预期的输出应该是,有人可以帮助或指导我吗:
id A_Pre A_Post B_Pre B_Post C_Pre C_Post
0 1 20 10 0 1 1 1
1 2 23 20 1 1 2 1
【问题讨论】:
-
sort_index(axis='columns') -
@ifly6 注意
Pre和Post,"o" < "r"。
标签: python python-3.x pandas dataframe python-2.7