为了账号安全,请及时绑定邮箱和手机立即绑定

pandas真正的外连接?

pandas真正的外连接?

喵喵时光机 2024-01-12 10:40:32
如何在 pandas 中获得真正的外部连接?这意味着它实际上为您提供了整个输出,而不是组合要合并的列。在我看来,这有点愚蠢,因为它使得很难确定要连续执行哪种操作。我一直这样做是为了检测是否应该插入、更新或删除数据,但是我总是必须在列上创建额外的合并副本,这只是某些数据集上的大量开销(有时是大量开销)。例子:import pandas as pdkeys = ["A","B"]df1 = pd.DataFrame({"A":[1,2,3],"B":["one","two","three"],"C":["testThis","testThat", "testThis"],"D":[None,hash("B"),hash("C")]})df2 = pd.DataFrame({"A":[2,3,4],"B":["two","three","four"],"C":["testThis","testThat", "testThis"], "D":[hash("G"),hash("C"),hash("D")]})fullJoinDf = df1.merge(df2, how="outer", left_on=keys, right_on=keys, suffixes=["","_r"])display(    fullJoinDf,)    A   B       C           D               C_r          D_r0   1   one     testThis    NaN             NaN          NaN1   2   two     testThat    -3.656526e+18   testThis    -9.136326e+182   3   three   testThis    -8.571400e+18   testThat    -8.571400e+183   4   four    NaN         NaN             testThis    -4.190116e+17注意到它如何输出A并B神奇地组合成一组列。我想要的是在 SQL 外连接等中得到的结果,例如:    A    B      C           D               A_r  B_r     C_r        D_r0   1    one    testThis    NaN             NaN  NaN     NaN        NaN     1   2    two    testThat    -3.656526e+18   2    two     testThis   -9.136326e+182   3    three  testThis    -8.571400e+18   3    three   testThat   -8.571400e+183   NaN  NaN    NaN         NaN             4    four    testThis   -4.190116e+17编辑@Felipe Whitaker使用连接:df3 = df1.copy().set_index(keys)df4 = df2.copy().set_index(keys)t = pd.concat([df3,df4], axis=1)t.reset_index(),     A   B       C           D               C           D0   1   one     testThis    NaN             NaN         NaN1   2   two     testThat    -3.656526e+18   testThis    -9.136326e+182   3   three   testThis    -8.571400e+18   testThat    -8.571400e+183   4   four    NaN         NaN             testThis    -4.190116e+17编辑示例*鉴于答案,我将发布更多测试,因此任何其他偶然发现此问题的人都可以看到我在执行此操作时发现的更多“gatcha”变体。
查看完整描述

2 回答

?
慕的地8271018

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


查看完整回答
反对 回复 2024-01-12
?
ibeautiful

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


查看完整回答
反对 回复 2024-01-12
  • 2 回答
  • 0 关注
  • 58 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信