11
$\begingroup$

I have two data frames. So how do I remove rows that have identical email addresses in df2 from df1?

>>df1
   First  Last  Email
0 Adam   Smith  email@email.com
1 John   Brown  email2@email.com
2 Joe    Max    email3@email.com
3 Will   Bill   email4@email.com


>>df2
  First  Last   Email
0 Adam   Smith  email@email.com
1 John   Brown  email2@email.com
$\endgroup$
0
12
$\begingroup$

You can try this:

cond = df1['Email'].isin(df2['Email'])
df1.drop(df1[cond].index, inplace = True)

>>df1
    First   Last    Email
2   Joe     Max     email3@email.com
3   Will    Bill    email4@email.com
$\endgroup$
6
$\begingroup$

Simpler to use isin() with dropna()

https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.isin.html

df1[~df1.isin(df2)].dropna()
$\endgroup$
2
  • $\begingroup$ Quick question, what does the tilde stands for? $\endgroup$ Apr 23 '21 at 18:23
  • $\begingroup$ It's a negation. Will turn all True to False and vice versa. $\endgroup$
    – Idodo
    Apr 26 '21 at 8:50

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