Home > Blockchain >  How to filter with multiple conditions in a pandas dataframe that include unwanted entries?
How to filter with multiple conditions in a pandas dataframe that include unwanted entries?

Time:06-07

I currently have the following code:

df[(df.Edition == 2004) & (df.Discipline == "Athletics") | (df.Discipline == "Aquatics")]

However, this (rightfully) returns every entry where Edition is 2004, and discipline is EITHER Athletics or Aquatics (thus giving me entries where Edition is not 2004). However, I want to pull all entries where Discipline is Athletics OR Aquatics but the Edition is locked to 2004. How can I query that?

CodePudding user response:

Get your parenthesis right:

>>> df[(df.Edition == 2004) & 
       ((df.Discipline == "Athletics") | 
       (df.Discipline == "Aquatics"))]

CodePudding user response:

You can use isin

df[(df.Edition == 2004) & df.Discipline.isin(["Athletics", "Aquatics"])]
  • Related