我合并了两个具有多个重叠列的 Pandas 数据框。我想并排放置重叠的列。
merge = df1.merge(df2, how='outer')
输出:
A,B,C,D,A_x,B_x,C_x,D_x
我希望输出为:
A,A_x,B,B_x,C,C_x,D,D_x
我可以明确地做到这一点,但我有很多专栏并且想要一个“动态”解决方案。
最佳答案
使用.sort_index(axis=1)
:
result = df1.merge(df2, how='outer').sort_index(axis=1)
关于合并后python pandas对列进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47561843/