系列的真值是不明確的。使用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天全站免登陸
全免费A级毛片免费看无码播放