Not registered yet?
Register now! It is easy and done in 1 minute and gives you access to special discounts and much more!
I would like the element-wise logical OR operator. I know "or" itself is not what I am looking for.
I am aware that AND corresponds to & and NOT, ~. But what about OR?
The corresponding operator is |:
df[(df < 3) | (df == 5)]
would elementwise check if value is less than 3 or equal to 5.
If you need a function to do this, we have np.logical_or. For two conditions, you can use
Or, for multiple conditions use the logical_or.reduce,
Since the conditions are specified as individual arguments, parentheses grouping is not needed.
More information on logical operations with pandas can be found here.
The round brackets are important
| and np.logical_or behave differently in the presence of NaNs. See stackoverflow.com/q/37131462/2596586
To take the element-wise logical OR of two Series a and b just do
a | b