pandas 中的元素级逻辑或 [英] Element-wise logical OR in Pandas
问题描述
我想要逐个元素的逻辑OR运算符.我知道或"本身不是我想要的.
I would like the element-wise logical OR operator. I know "or" itself is not what I am looking for.
我知道AND对应&
而不是~
.但是OR呢?
I am aware that AND corresponds to &
and NOT, ~
. But what about OR?
推荐答案
相应的运算符为|
:
df[(df < 3) | (df == 5)]
将按元素检查值是否小于3或等于5.
would elementwise check if value is less than 3 or equal to 5.
如果您需要执行此操作的功能,则可以 np.logical_or
.对于两种情况,您可以使用
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)]
或者,对于多种情况,请使用logical_or.reduce
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.
这篇关于 pandas 中的元素级逻辑或的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!