系列的真值是不明确的。使用a.empty,a.bool(),a.item(),a.any()或a.all() [英] Truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()

查看:20263
本文介绍了系列的真值是不明确的。使用a.empty,a.bool(),a.item(),a.any()或a.all()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用或条件过滤我的结果数据框。我想要我的结果df来提取0.25以上-0.25以下的所有列 var 值。下面的逻辑给出了一个模糊的真值,但是当我将这个过滤分为两个单独的操作时,它的工作原理。这里发生了什么?不知道在哪里使用建议的a.empty(),a.bool(),a.item(),a.any()或a.all()。

  result = result [(result ['var']> 0.25)or(result ['var'] <-0.25)] 


解决方案

python语句需要 true -values。对于熊猫,这些被认为是含糊的,所以你应该使用bitwise | (或)或 & (和)操作:

  result = result [(result ['var']> ; 0.25)| (result ['var'] <-0.25)] 

这些类型的数据结构都被重载产生元素方面的(或)。





只需为此语句添加更多解释:



当您要获取 bool 一个 pandas.Series

 >>>将大熊猫导入为pd 
>>> x = pd.Series([1])$ ​​b $ b>>> bool(x)
ValueError:Series的真值是不明确的。使用a.empty,a.bool(),a.item(),a.any()或a.all()。

你打的是一个运算符隐式将操作数转换为 bool (你使用,但也发生在如果):

 >>> x或x 
ValueError:Series的真值是不明确的。使用a.empty,a.bool(),a.item(),a.any()或a.all()。
>>> x和x
ValueError:Series的真值是不明确的。使用a.empty,a.bool(),a.item(),a.any()或a.all()。
>>>如果x:
... print('fun')
ValueError:Series的真值是不明确的。使用a.empty,a.bool(),a.item(),a.any()或a.all()。
>>>而x:
... print('fun')
ValueError:Series的真值是不明确的。使用a.empty,a.bool(),a.item(),a.any()或a.all()。

除了这4个语句,还有几个python函数隐藏了一些 bool 呼叫(如 any all filter ,...)这些通常没有问题, pandas.Series ,但为了完整,我想提到这些。






在您的情况下,异常并不真正有用,因为它没有提及正确的替代方案。对于,您可以使用(如果您想要元素比较):




  • numpy.logical_or

     >>>导入numpy为np 
    >>>> np.logical_or(x,y)

    或只是 | operator:

     >>> x | y 


  • numpy.logical_and

     >>> np.logical_and(x,y)

    或只是& code> operator:

     >>> x& y 




如果您使用的是运算符由于运算符优先级,请确保您正确设置了圆括号。



几个逻辑numpy函数,其中应该在 pandas.Series 上工作。






如果您在或之间执行时遇到的替代方案更适合。我稍后会解释一下:




  • 如果你想检查你的系列是否是

     >>> x = pd.Series([])
    >>> x.empty
    True
    >>> x = pd.Series([1])$ ​​b $ b>>> x.empty
    False

    Python通常解释 len gth的容器(如 list tuple ,...)作为真值,如果没有明确的布尔解释。所以如果你想要这样的python样检查,你可以这样做:如果x.size 如果不是x.empty 而不是如果x


  • 如果您的系列包含一个且只有一个布尔值:

     >>> x = pd.Series([100])
    >>> (x> 50).bool()
    True
    >>> (x< 50).bool()
    False


  • 检查系列的第一个也是唯一的项(例如 .bool(),但是对于不是布尔值的内容而言):

     >>> x = pd.Series([100])
    >>> x.item()
    100


  • 如果要检查全部任何项目不为零,不为空或不为False:

     >>> x = pd.Series([0,1,2])
    >>> x.all()#因为一个元素是零
    False
    >>>> x.any()#因为一个(或多个)元素不为零
    True



    • Having issue filtering my result dataframe with an or condition. I want my result df to extract all column var values that are above 0.25 and below -0.25. This logic below gives me an ambiguous truth value however it work when I split this filtering in two separate operations. What is happening here? not sure where to use the suggested a.empty(), a.bool(), a.item(),a.any() or a.all().

       result = result[(result['var']>0.25) or (result['var']<-0.25)]
      

      解决方案

      The or and and python statements require truth-values. For pandas these are considered ambiguous so you should use "bitwise" | (or) or & (and) operations:

      result = result[(result['var']>0.25) | (result['var']<-0.25)]
      

      These are overloaded for these kind of datastructures to yield the element-wise or (or and).


      Just to add some more explanation to this statement:

      The exception is thrown when you want to get the bool of a pandas.Series:

      >>> import pandas as pd
      >>> x = pd.Series([1])
      >>> bool(x)
      ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
      

      What you hit was a place where the operator implicitly converted the operands to bool (you used or but it also happens for and, if and while):

      >>> x or x
      ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
      >>> x and x
      ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
      >>> if x:
      ...     print('fun')
      ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
      >>> while x:
      ...     print('fun')
      ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
      

      Besides these 4 statements there are several python functions that hide some bool calls (like any, all, filter, ...) these are normally not problematic with pandas.Series but for completeness I wanted to mention these.


      In your case the exception isn't really helpful, because it doesn't mention the right alternatives. For and and or you can use (if you want element-wise comparisons):

      • numpy.logical_or:

        >>> import numpy as np
        >>> np.logical_or(x, y)
        

        or simply the | operator:

        >>> x | y
        

      • numpy.logical_and:

        >>> np.logical_and(x, y)
        

        or simply the & operator:

        >>> x & y
        

      If you're using the operators then make sure you set your parenthesis correctly because of the operator precedence.

      There are several logical numpy functions which should work on pandas.Series.


      The alternatives mentioned in the Exception are more suited if you encountered it when doing if or while. I'll shortly explain each of these:

      • If you want to check if your Series is empty:

        >>> x = pd.Series([])
        >>> x.empty
        True
        >>> x = pd.Series([1])
        >>> x.empty
        False
        

        Python normally interprets the length of containers (like list, tuple, ...) as truth-value if it has no explicit boolean interpretation. So if you want the python-like check, you could do: if x.size or if not x.empty instead of if x.

      • If your Series contains one and only one boolean value:

        >>> x = pd.Series([100])
        >>> (x > 50).bool()
        True
        >>> (x < 50).bool()
        False
        

      • If you want to check the first and only item of your Series (like .bool() but works even for not boolean contents):

        >>> x = pd.Series([100])
        >>> x.item()
        100
        

      • If you want to check if all or any item is not-zero, not-empty or not-False:

        >>> x = pd.Series([0, 1, 2])
        >>> x.all()   # because one element is zero
        False
        >>> x.any()   # because one (or more) elements are non-zero
        True
        

      这篇关于系列的真值是不明确的。使用a.empty,a.bool(),a.item(),a.any()或a.all()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆