By Keith


2014-07-16 08:19:48 8 Comments

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?

2 comments

@deinonychusaur 2014-07-16 08:24:41

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

df[np.logical_or(df<3, df==5)]

Or, for multiple conditions use the logical_or.reduce,

df[np.logical_or.reduce([df<3, df==5])]

Since the conditions are specified as individual arguments, parentheses grouping is not needed.

More information on logical operations with pandas can be found here.

@Gerard 2016-08-08 15:22:43

The round brackets are important

@Frank 2019-11-14 00:18:04

| and np.logical_or behave differently in the presence of NaNs. See stackoverflow.com/q/37131462/2596586

@alanwsx 2020-03-15 00:17:13

Just a comment: or is not working here. Only | works.

@Jonathan Stray 2017-05-12 21:35:45

To take the element-wise logical OR of two Series a and b just do

a | b

Related Questions

Sponsored Content

27 Answered Questions

[SOLVED] Renaming columns in pandas

64 Answered Questions

[SOLVED] How do you split a list into evenly sized chunks?

18 Answered Questions

[SOLVED] Selecting multiple columns in a pandas dataframe

22 Answered Questions

[SOLVED] How to iterate over rows in a DataFrame in Pandas

15 Answered Questions

[SOLVED] Delete column from pandas DataFrame

12 Answered Questions

[SOLVED] Getting the last element of a list

  • 2009-05-30 19:28:53
  • Janusz
  • 2191558 View
  • 2125 Score
  • 12 Answer
  • Tags:   python list indexing

26 Answered Questions

[SOLVED] Pythonic way to create a long multi-line string

19 Answered Questions

[SOLVED] Get list from pandas DataFrame column headers

16 Answered Questions

[SOLVED] "Large data" work flows using pandas

10 Answered Questions

Sponsored Content