Element-wise logical OR in Pandas

核能气质少年 提交于 2019-11-26 12:28:47

问题


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?


回答1:


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.




回答2:


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

a | b


来源:https://stackoverflow.com/questions/24775648/element-wise-logical-or-in-pandas

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!