Reputation: 185
I have a sample dataframe df
City | Flow |
---|---|
Berlin | True |
Berlin | False |
Berlin | False |
Munich | True |
Munich | False |
Frankfurt | True |
Frankfurt | False |
Amsterdam | True |
Amsterdam | False |
I want to filter dataframe df1 where Flow column is True for all cities except Frankfurt and Amsterdam such that df becomes
City | Flow |
---|---|
Berlin | True |
Munich | True |
Frankfurt | True |
Frankfurt | False |
Amsterdam | True |
Amsterdam | False |
Upvotes: 0
Views: 108
Reputation: 862511
Use Series.isin
chained by boolean mask in column Flow
by |
for bitwise OR
and filter in boolean indexing
:
df = df[df['Flow'] | df['City'].isin(['Frankfurt', 'Amsterdam'])]
print (df)
City Flow
0 Berlin True
3 Munich True
5 Frankfurt True
6 Frankfurt False
7 Amsterdam True
8 Amsterdam False
Upvotes: 0
Reputation: 458
You can use conditional selection:
df[ (df.Flow==True) | (df.City.isin(['Frankfurt', 'Amsterdam']))]
Output:
City Flow
0 Berlin True
3 Munich True
5 Frankfurt True
6 Frankfurt False
7 Amsterdam True
8 Amsterdam False
Upvotes: 1