Selecting rows of a dataframe based on two conditions in Pandas python

Use | instead of or. So:

df.loc[(cond1) | (cond2), :]

The or operator wants to compare two boolean values (or two expression that evaluate to True or False). But a Series (or numpy array) does not simply evaluates to True or False, and in this case we want to compare both series element-wise. For this you can use | which is called 'bitwise or'.

Pandas follows here the numpy conventions. See here in the pandas docs for an explanation on it.


The condition should be as follows

df.loc[(cond1) | (cond2)]

Each condition has to be enclosed in parentheses as well. High priority is given for parentheses than the bitwise 'OR' operator. When the parentheses are not provided it would also give the same error

Tags:

Python

Pandas