0

I would like to do this:

df[(df["Class"] == 'Class1' or  'Class2')]

i.e. find rows with class 1 or 2, but can't find the correct syntax in the documentation.

William Baker Morrison
  • 1,642
  • 4
  • 21
  • 33

2 Answers2

3

Better is use isin:

df[df["Class"].isin(['Class1','Class2'])]

Or use | for bitwise or with () because priority precedence of operators with boolean indexing:

df[(df["Class"] == 'Class1') |  (df["Class"] == 'Class2')]

Another solutions with query:

df.query("Class == ['Class1', 'Class2']")

Or:

df.query("Class == 'Class1' |  Class == 'Class2'")
#here working or too
#df = df.query("Class == 'Class1' or  Class == 'Class2'")
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
0

These are the options

df[(df["Class"] == 'Class1') |  (df["Class"] =='Class2')]

or

df[df["Class"].isin(['Class1','Class2'])]
rpanai
  • 12,515
  • 2
  • 42
  • 64