Element-wise logical OR in Pandas

前端 未结 2 1837
有刺的猬
有刺的猬 2020-11-29 05:56

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,

相关标签:
2条回答
  • 2020-11-29 06:09

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

    a | b
    
    0 讨论(0)
  • 2020-11-29 06:26

    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.

    0 讨论(0)
提交回复
热议问题