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.

0

2 Answers 2

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'")
Sign up to request clarification or add additional context in comments.

Comments

0

These are the options

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

or

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.