Pandas boolean DataFrame selection ambiguity

后端 未结 3 531
一整个雨季
一整个雨季 2021-01-15 22:50

EDIT: Fixed values in tables.

Let\'s say I have a pandas dataframe df:

>>>df
                  a         b         c
        0  0.016367  0.         


        
3条回答
  •  星月不相逢
    2021-01-15 23:30

    It is not possible for custom types to override the behavior of and and or in Python. That is, it is not possible for Numpy to say that it wants [0, 1, 1] and [1, 1, 0] to be [0, 1, 0]. This is because of how the and operation short-circuits (see the documentation); in essence, the short-circuiting behavior of and and or means that these operations must work as two separate truth values on the two arguments; they cannot combine their two operands in some way that makes use of data in both operands at once (for instance, to compare the elements componentwise, as would be natural for Numpy).

    The solution is to use the bitwise operators & and |. However, you do have to be careful with this, since the precedence is not what you might expect.

提交回复
热议问题