2016-10-05 10 views

Antwort

5

Sie können mit isinboolean indexing und Zustand verwenden, Invertierung boolean Series ist durch ~:

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] 

Eine andere Lösung mit 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.ix[df._merge == 'left_only', ['email']]) 
    email 
1 [email protected] 
2 [email protected] 
3 [email protected] 
+0

Ausgezeichnet! Ich bin mit beiden Antworten zufrieden, aber die "Merge" -Lösung scheint einfacher als die erste zu sein. Außerdem habe ich festgestellt, dass der "Indikator" -Parameter mir bei verwandten Problemen sehr helfen kann. Danke nochmal. – Vini

+0

Froh kann dir helfen! – jezrael

Verwandte Themen