系列的真值不明确.使用 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()

查看:65
本文介绍了系列的真值不明确.使用 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)]

解决方案

orand python 语句需要 truth-values.对于 pandas,这些被认为是不明确的,所以你应该使用按位"|(或)或 &(和)操作:

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

这些类型的数据结构被重载以产生元素级的or(或and).

<小时>

只是对这个声明添加一些更多的解释:

当你想获取pandas.Seriesbool时抛出异常:

<预><代码>>>>将熊猫导入为 pd>>>x = pd.Series([1])>>>布尔(x)ValueError:系列的真值不明确.使用 a.empty、a.bool()、a.item()、a.any() 或 a.all().

你击中的是运算符隐式将操作数转换为bool的地方(你使用了or,但它也发生在>andifwhile):

<预><代码>>>>x 或 xValueError:系列的真值不明确.使用 a.empty、a.bool()、a.item()、a.any() 或 a.all().>>>x 和 xValueError:系列的真值不明确.使用 a.empty、a.bool()、a.item()、a.any() 或 a.all().>>>如果 x:...打印('有趣')ValueError:系列的真值不明确.使用 a.empty、a.bool()、a.item()、a.any() 或 a.all().>>>而 x:...打印('有趣')ValueError:系列的真值不明确.使用 a.empty、a.bool()、a.item()、a.any() 或 a.all().

除了这 4 条语句之外,还有几个 Python 函数隐藏了一些 bool 调用(例如 anyallfilter, ...) 这些对于 pandas.Series 通常没有问题,但为了完整起见,我想提一下这些.

<小时>

在您的情况下,例外并没有真正的帮助,因为它没有提到正确的替代方案.对于 andor,您可以使用(如果您想要按元素进行比较):

  • numpy.logical_or:

    <预><代码>>>>将 numpy 导入为 np>>>np.logical_or(x, y)

    或简单的 | 运算符:

    <预><代码>>>>× |是

  • numpy.logical_and:

    <预><代码>>>>np.logical_and(x, y)

    或简单的 & 运算符:

    <预><代码>>>>x &是

如果您使用运算符,请确保正确设置括号,因为运算符优先级.

几个逻辑 numpy 函数应该pandas.Series 上工作.

<小时>

如果您在执行 ifwhile 时遇到它,则 Exception 中提到的替代方案更适合.我将简要解释其中的每一个:

  • 如果您想检查您的系列是否:

    <预><代码>>>>x = pd.Series([])>>>x.空真的>>>x = pd.Series([1])>>>x.空错误的

    Python 通常将容器的 length(如 listtuple、...)解释为真值,如果它没有明确的布尔解释.所以如果你想要类似 python 的检查,你可以这样做:if x.sizeif not x.empty 而不是 if x.

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

    <预><代码>>>>x = pd.Series([100])>>>(x > 50).bool()真的>>>(x <50).bool()错误的

  • 如果您想检查系列的第一个也是唯一的项目(例如 .bool(),但即使对于非布尔内容也适用):<预><代码>>>>x = pd.Series([100])>>>x.item()100

  • 如果您想检查 allany 项目是否非零、非空或非假:

    <预><代码>>>>x = pd.Series([0, 1, 2])>>>x.all() # 因为一个元素为零错误的>>>x.any() # 因为一个(或多个)元素非零真的

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天全站免登陆