Reputation: 59
I'm trying to filter a dataframe based on the following condition:
colA = alpha OR beta
colB = gamma
I've tried the following
filtered_df = (df[(df['colA'] == 'alpha') & (df['colB'] == 'gamma')]) | (df[(df['colA'] == 'beta') & (df['colB'] == 'gamma')])
But this doesn't work and I get the following error message :
TypeError: unsupported operand type(s) for |: 'str' and 'bool'
What would be the most pythonic way to achieve the desired outcome?
Thanks!
Upvotes: 1
Views: 753
Reputation: 151
You can try something like this.
df.loc[((df['colA'] == 'alpha') | (df['colA'] == 'beta')) & (df['colB'] == 'gamma')]
Upvotes: 1
Reputation: 24314
You can use try loc
+isin()
+eq()
:
filtered_df =df.loc[(df['colA'].isin(['alpha','beta'])) & (df['colB'].eq('gamma'))]
Now If you print filtered_df
you will get your filtered dataframe
Upvotes: 3