Pandas analogue of the SQL statement "NOT IN"

Surprisingly, I cannot find an analogue of the "NOT IN" SQL statement in pandas DataFrames.

A = pd.DataFrame({'a':[6,8,3,9,5],
                       'b':['II','I','I','III','II']})

B = pd.DataFrame({'c':[1,2,3,4,5]})

      

I want all lines from A

that A

do not contain values ​​from B

c

. Something like:

A = A[ A.a not in B.c]

      

+3


source to share


1 answer


I think you are really close - need isin

s ~

for negative boolean mask - list

use instead Series

B.c

:



print (~A.a.isin(B.c))
0     True
1     True
2    False
3     True
4    False
Name: a, dtype: bool

A = A[~A.a.isin(B.c)]
print (A)
   a    b
0  6   II
1  8    I
3  9  III

      

+3


source







All Articles