2

I have a dataframe with an email column. I'm trying delete all records from this dataframe where the email address exists in a second dataframe.

In SQL this would be

delete from df1 where email in (select email from df2)

Thanks

1
  • Are the dataframes indexed identically? Or do they share a unique identifier? We'll probably end up using the pd.drop() command, but it depends on how the two are related.
    – 3novak
    Jun 24, 2017 at 2:08

3 Answers 3

1

You can use ~isin

In [30]: df1
Out[30]: 
   pid
0    1
1    2
2    3
3    4

In [31]: df2
Out[31]: 
   pid
0    1
1    2

In [32]: df1=df1[~df1['pid'].isin(df2['pid'])]

In [33]: df1
Out[33]: 
   pid
2    3
3    4
1

You can do following using where:

import pandas as pd

df1 = pd.DataFrame({'email':['[email protected]','[email protected]','[email protected]']})
df2 = pd.DataFrame({'email':['[email protected]']})

print(df1)

Output for df1:

          email
0  [email protected]
1  [email protected]
2  [email protected]

Output for print(df2):

     email
 0  [email protected]

Now, using where:

df1 = df1.where(~df1.email.isin(df2.email)).dropna()
print(df1)

Output:

          email
1  [email protected]
2  [email protected]
0

You can use isin with drop too. Using boolean indexing and .index will drop those records form the df1 dataframe. Borrowing open-source's setup

df1 = df1.drop(df1[df1.email.isin(df2.email)].index)

Output:

          email
1  [email protected]
2  [email protected]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.