如何在Pandas中进行左外连接,排除交集?
我有两个Pandas数据框:
df1 = pd.DataFrame(data = {'col1' : ['finance', 'finance', 'finance', 'accounting', 'IT'], 'col2' : ['az', 'bh', '', '', '']})
df2 = pd.DataFrame(data = {'col1' : ['finance', 'finance', 'finance', 'finance', 'finance'], 'col2' : ['', 'az', '', '', '']})
df1
col1 col2
0 finance az
1 finance bh
2 finance
3 accounting
4 IT
df2
col1 col2
0 finance
1 finance az
2 finance
3 finance
4 finance
正如您所看到的,数据框中也有空值。我尝试使用示例,但它没有给我想要的结果。
common = df1.merge(df2,on=['col1','col2'])
df3=df1[(~df1.col1.isin(common.col1))&(~df1.col2.isin(common.col2))]
我希望输出看起来像这样。
col1 col2
3 accounting
4 IT
common = pd.merge(df1, df2, how='left', on=['col1', 'col2'])
吗? - mrhallak