2 回答
TA贡献1796条经验 获得超4个赞
如果您根本不关心原始索引:
df1.index = df1[keys]
df2.index = df2[keys]
fullJoinDf = df1.merge(df2, how="outer", left_index=True, right_index=True, suffixes=["","_r"])
结果:
A B C D A_r B_r C_r D_r
0 1.0 one testThis NaN NaN NaN NaN NaN
1 2.0 two testThat 6.368540e+18 2.0 two testThis -6.457388e+18
2 3.0 three testThis -7.490461e+18 3.0 three testThat -7.490461e+18
3 NaN NaN NaN NaN 4.0 four testThis 4.344649e+18
TA贡献1993条经验 获得超5个赞
如果您在 1 个 DataFrame 中重命名合并中使用的列,merge它看起来会给出正确的答案
df1.merge(df2.rename({'A': 'A_y', 'B': 'B_y'}, axis =1), left_on=keys, right_on=['A_y', 'B_y'], how='outer')
#output:
A B C_x D_x A_y B_y C_y D_y
0 1.0 one testThis NaN NaN NaN NaN NaN
1 2.0 two testThat -2.482945e+18 2.0 two testThis -1.215774e+18
2 3.0 three testThis 1.140152e+17 3.0 three testThat 1.140152e+17
3 NaN NaN NaN NaN 4.0 four testThis -4.915382e+18
添加回答
举报