I have 2 Data Frames, one named USERS and another named EXCLUDE. Both of them have a field named "email".
Basically, I want to remove every row in USERS that has an email contained in EXCLUDE.
How can I do it?
To remove rows from a data frame that exists in another data frame, we can use subsetting with single square brackets. This removal will help us to find the unique rows in the data frame based on the column of another data frame.
Use pandas. DataFrame. drop() method to delete/remove rows with condition(s).
subtract() function is used for finding the subtraction of dataframe and other, element-wise. This function is essentially same as doing dataframe – other but with a support to substitute for missing data in one of the inputs.
You can use boolean indexing
and condition with isin
, inverting boolean Series
is by ~
:
import pandas as pd USERS = pd.DataFrame({'email':['[email protected]','[email protected]','[email protected]','[email protected]','[email protected]']}) print (USERS) email 0 [email protected] 1 [email protected] 2 [email protected] 3 [email protected] 4 [email protected] EXCLUDE = pd.DataFrame({'email':['[email protected]','[email protected]']}) print (EXCLUDE) email 0 [email protected] 1 [email protected]
print (USERS.email.isin(EXCLUDE.email)) 0 True 1 False 2 False 3 False 4 True Name: email, dtype: bool print (~USERS.email.isin(EXCLUDE.email)) 0 False 1 True 2 True 3 True 4 False Name: email, dtype: bool print (USERS[~USERS.email.isin(EXCLUDE.email)]) email 1 [email protected] 2 [email protected] 3 [email protected]
Another solution with merge
:
df = pd.merge(USERS, EXCLUDE, how='outer', indicator=True) print (df) email _merge 0 [email protected] both 1 [email protected] left_only 2 [email protected] left_only 3 [email protected] left_only 4 [email protected] both print (df.loc[df._merge == 'left_only', ['email']]) email 1 [email protected] 2 [email protected] 3 [email protected]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With