ValueError: The truth value of a Series is ambiguous

后端 未结 2 1279
别那么骄傲
别那么骄傲 2020-12-11 10:38
>>> df.head()
                         № Summer  Gold  Silver  Bronze  Total  № Winter  \\
Afghanistan (AFG)              13     0       0       2      2            


        
相关标签:
2条回答
  • 2020-12-11 11:10

    Assuming we have the following DF:

    In [35]: df
    Out[35]:
       a  b  c
    0  9  0  1
    1  7  7  4
    2  1  8  9
    3  6  7  5
    4  1  4  6
    

    The following command:

    df.a > 5 | df.b > 5
    

    because | has higher precedence (compared to >) as it's specified in the Operator precedence table) it will be translated to:

    df.a > (5 | df.b) > 5
    

    which will be translated to:

    df.a > (5 | df.b) and (5 | df.b) > 5
    

    step by step:

    In [36]: x = (5 | df.b)
    
    In [37]: x
    Out[37]:
    0     5
    1     7
    2    13
    3     7
    4     5
    Name: c, dtype: int32
    
    In [38]: df.a > x
    Out[38]:
    0     True
    1    False
    2    False
    3    False
    4    False
    dtype: bool
    
    In [39]: x > 5
    Out[39]:
    0    False
    1     True
    2     True
    3     True
    4    False
    Name: b, dtype: bool
    

    but the last operation won't work:

    In [40]: (df.a > x) and (x > 5)
    ---------------------------------------------------------------------------
    ...
    skipped
    ...
    ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
    

    The error message above might lead inexperienced users to do something like this:

    In [12]: (df.a > 5).all() | (df.b > 5).all()
    Out[12]: False
    
    In [13]: df[(df.a > 5).all() | (df.b > 5).all()]
    ...
    skipped
    ...
    KeyError: False
    

    But in this case you just need to set your precedence explicitly in order to get expected result:

    In [10]: (df.a > 5) | (df.b > 5)
    Out[10]:
    0     True
    1     True
    2     True
    3     True
    4    False
    dtype: bool
    
    In [11]: df[(df.a > 5) | (df.b > 5)]
    Out[11]:
       a  b  c
    0  9  0  1
    1  7  7  4
    2  1  8  9
    3  6  7  5
    
    0 讨论(0)
  • 2020-12-11 11:27

    This is the real reason for the error:

    http://pandas.pydata.org/pandas-docs/stable/gotchas.html

    pandas follows the numpy convention of raising an error when you try to convert something to a bool. This happens in a if or when using the boolean operations, and, or, or not. It is not clear what the result of

    >>> if pd.Series([False, True, False]):
         ...
    

    should be. Should it be True because it’s not zero-length? False because there are False values? It is unclear, so instead, pandas raises a ValueError:

    >>> if pd.Series([False, True, False]):
        print("I was true")
    Traceback
        ...
    ValueError: The truth value of an array is ambiguous. Use a.empty, a.any() or a.all().
    

    If you see that, you need to explicitly choose what you want to do with it (e.g., use any(), all() or empty). or, you might want to compare if the pandas object is None

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