熊猫左外连接排除

Ent*_*ast 5 python left-join pandas

如何在 Pandas 中进行左外连接(不包括交点)?

我有 2 个熊猫数据框

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', '', '', '']})
Run Code Online (Sandbox Code Playgroud)

df1

    col1    col2
0   finance az
1   finance bh
2   finance 
3   accounting  
4   IT  
Run Code Online (Sandbox Code Playgroud)

df2

    col1    col2
0   finance 
1   finance az
2   finance 
3   finance 
4   finance 
Run Code Online (Sandbox Code Playgroud)

如您所见,数据框也有空白值。我尝试使用这个例子,但它没有给我想要的结果。

common = df1.merge(df2,on=['col1','col2'])
df3=df1[(~df1.col1.isin(common.col1))&(~df1.col2.isin(common.col2))]
Run Code Online (Sandbox Code Playgroud)

我希望输出看起来像

    col1    col2
3   accounting  
4   IT  
Run Code Online (Sandbox Code Playgroud)

Bin*_*Bin 9

通过设置pandas merge的indicator=True,可以实现pandas left外连接排除。然后按 _merge 列中的指标过滤。

df=pd.merge(df1,df2[['col1']],on=['col1'],how="outer",indicator=True)
df=df[df['_merge']=='left_only']
# this following line is just formating
df = df.reset_index()[['col1', 'col2']] 
Run Code Online (Sandbox Code Playgroud)

输出:

col1    col2
0   accounting  
1   IT  
Run Code Online (Sandbox Code Playgroud)

==================================

====以下是显示机制的示例====

df1 = pd.DataFrame({'key1': ['0', '1'],
                     'key2': [-1, -1],
                     'A': ['A0', 'A1'],
                     })


df2 = pd.DataFrame({'key1': ['0', '1'],
                      'key2': [1, -1], 
                    'B': ['B0', 'B1']
                     })
Run Code Online (Sandbox Code Playgroud)

df1
Run Code Online (Sandbox Code Playgroud)

输出:

    A   key1    key2
0   A0  0       -1
1   A1  1       -1
Run Code Online (Sandbox Code Playgroud)

df2
Run Code Online (Sandbox Code Playgroud)

输出:

    B   key1    key2
0   B0  0       1
1   B1  1       -1
Run Code Online (Sandbox Code Playgroud)

df=pd.merge(df1,df2,on=['key1','key2'],how="outer",indicator=True)
Run Code Online (Sandbox Code Playgroud)

输出:

     A  key1    key2    B   _merge
0   A0  0   -1  NaN left_only
1   A1  1   -1  B1  both
2   NaN 0   1   B0  right_only
Run Code Online (Sandbox Code Playgroud)

: 列有以上指标_merge。您可以在一个数据框中选择行,但不能在另一个数据框中选择行。

df=df[df['_merge']=='left_only']
df
Run Code Online (Sandbox Code Playgroud)

输出:

    A   key1    key2    B   _merge
0   A0  0   -1  NaN left_only
Run Code Online (Sandbox Code Playgroud)


EFT*_*EFT 0

col1这会失败,因为您独立检查&中的匹配项col2,并排除其中任何一个的匹配项。空字符串与行中的空字符串匹配finance

你想要:

df3 = df1[(~df1.col1.isin(common.col1))|(~df1.col2.isin(common.col2))]
df3
Out[150]: 
         col1 col2
1     finance   bh
3  accounting     
4          IT  
Run Code Online (Sandbox Code Playgroud)

df1要获取不在 中的行df2

为了具体得到

df3
    col1    col2
3   accounting  
4   IT  
Run Code Online (Sandbox Code Playgroud)

您可以尝试仅选择那些不匹配的col1.

df3 = df1[~df1.col1.isin(df2.col1)]
df3
Out[172]: 
         col1 col2
3  accounting     
4          IT
Run Code Online (Sandbox Code Playgroud)

要独立检查col1&中的匹配col2并排除其中任一匹配,同时让NaNs 比较不相等/从不算作匹配,您可以使用

df3 = df1[(~df1.col1.isin(common.col1)|df1.col1.isnull())&(~df1.col2.isin(common.col2)|df1.col2.isnull())]
df3
Out[439]: 
         col1 col2
3  accounting  NaN
4          IT  NaN
Run Code Online (Sandbox Code Playgroud)

假设您正在实际数据中使用实际的NaNs 或Nonenp.nan而不是像本示例中那样的空字符串。如果是后者,您需要添加

df1.replace('', np.nan, inplace=True)
df2.replace('', np.nan, inplace=True)
Run Code Online (Sandbox Code Playgroud)

第一的。