第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

pandas真正的外連接?

pandas真正的外連接?

喵喵時光機(jī) 2024-01-12 10:40:32
如何在 pandas 中獲得真正的外部連接?這意味著它實際上為您提供了整個輸出,而不是組合要合并的列。在我看來,這有點愚蠢,因為它使得很難確定要連續(xù)執(zhí)行哪種操作。我一直這樣做是為了檢測是否應(yīng)該插入、更新或刪除數(shù)據(jù),但是我總是必須在列上創(chuàng)建額外的合并副本,這只是某些數(shù)據(jù)集上的大量開銷(有時是大量開銷)。例子: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 外連接等中得到的結(jié)果,例如:    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編輯示例*鑒于答案,我將發(fā)布更多測試,因此任何其他偶然發(fā)現(xiàn)此問題的人都可以看到我在執(zhí)行此操作時發(fā)現(xiàn)的更多“gatcha”變體。
查看完整描述

2 回答

?
慕的地8271018

TA貢獻(xiàn)1796條經(jīng)驗 獲得超4個贊

如果您根本不關(guān)心原始索引:


df1.index = df1[keys]

df2.index = df2[keys]


fullJoinDf = df1.merge(df2, how="outer", left_index=True, right_index=True, suffixes=["","_r"])

結(jié)果:


     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


查看完整回答
反對 回復(fù) 2024-01-12
?
ibeautiful

TA貢獻(xiàn)1993條經(jīng)驗 獲得超6個贊

如果您在 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


查看完整回答
反對 回復(fù) 2024-01-12
  • 2 回答
  • 0 關(guān)注
  • 212 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號