我想用or
条件过滤我的数据框,以保持特定列的值在范围[-0.25, 0.25]
之外的行。
df = df[(df['col'] < -0.25) or (df['col'] > 0.25)]
但我得到的错误:
Series 的真值不明确。请使用 a.empty 、 a.bool () 、 a.item () 、 a.any () 或 a.all ()

or
和and
python 语句需要truth
值。对于pandas
,这些被认为是不明确的,因此您应该使用“按位”|
(或)或&
(和)操作:
df = df[(df['col'] < -0.25) | (df['col'] > 0.25)]
这些对于这些类型的数据结构是重载的,以产生元素方式的or
或and
。
只是为了添加一些更多的解释这个语句:
当您想要获取pandas.Series
的bool
时,会引发异常:
>>> 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().
你打的是一个地方,运算符隐式将操作数转换为bool
(您使用or
,但它也发生在and
,if
和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().
除了这 4 条语句之外,还有几个 python 函数隐藏了一些bool
调用(如any
,all
,filter
,...)这些通常与pandas.Series
没有问题,但为了完整性,我想提到这些。
对于and
和or
,如果你想要元素级比较,你可以使用:
numpy.logical_or
:
>>> import numpy as np
>>> np.logical_or(x, y)
或者只是|
运算符:
>>> x | y
numpy.logical_and
:
>>> np.logical_and(x, y)
或者只是&
运算符:
>>> x & y
如果您使用的是运算符,那么一定要正确设置括号,因为operator precedence。
有several logical numpy functions,应该处理pandas.Series
。
如果您在执行if
或while
时遇到它,则 Exception 中提到的替代方案更适合。
如果要检查您的系列是否为空:
>>> x = pd.Series([])
>>> x.empty
True
>>> x = pd.Series([1])
>>> x.empty
False
如果没有明确的布尔解释,Python 通常将len
gth 容器(如list
,tuple
,...)解释为真值。因此,如果您想要类似 python 的检查,则可以执行以下操作:if x.size
或if not x.empty
而不是if x
。
如果您的Series
包含一个且只有一个布尔值:
>>> 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() # because one element is zero
False
>>> x.any() # because one (or more) elements are non-zero
True
熊猫使用按位&
|
,每个条件都应该包装在()
中
例如以下作品
data_query = data[(data['year'] >= 2005) & (data['year'] <= 2010)]
但是没有适当括号的相同查询不会
data_query = data[(data['year'] >= 2005 & data['year'] <= 2010)]
对于布尔逻辑,使用&
和|
。
np.random.seed(0)
df = pd.DataFrame(np.random.randn(5,3), columns=list('ABC'))
>>> df
A B C
0 1.764052 0.400157 0.978738
1 2.240893 1.867558 -0.977278
2 0.950088 -0.151357 -0.103219
3 0.410599 0.144044 1.454274
4 0.761038 0.121675 0.443863
>>> df.loc[(df.C > 0.25) | (df.C < -0.25)]
A B C
0 1.764052 0.400157 0.978738
1 2.240893 1.867558 -0.977278
3 0.410599 0.144044 1.454274
4 0.761038 0.121675 0.443863
要查看发生了什么,您将为每个比较获得一列布尔值,例如
df.C > 0.25
0 True
1 False
2 False
3 True
4 True
Name: C, dtype: bool
当您有多个条件时,您将返回多个列。这就是联接逻辑不明确的原因。使用and
或or
分别处理每个列,因此您首先需要将该列减少为单个布尔值。例如,查看每个列中的任何值或所有值是否为 True。
# Any value in either column is True?
(df.C > 0.25).any() or (df.C < -0.25).any()
True
# All values in either column is True?
(df.C > 0.25).all() or (df.C < -0.25).all()
False
实现相同功能的一种复杂方法是将所有这些列压缩在一起,并执行适当的逻辑。
>>> df[[any([a, b]) for a, b in zip(df.C > 0.25, df.C < -0.25)]]
A B C
0 1.764052 0.400157 0.978738
1 2.240893 1.867558 -0.977278
3 0.410599 0.144044 1.454274
4 0.761038 0.121675 0.443863
有关更多详细信息,请参考文档中的Boolean Indexing。

或者,您可以使用 Operator 模块。更多详细信息在这里Python docs
import operator
import numpy as np
import pandas as pd
np.random.seed(0)
df = pd.DataFrame(np.random.randn(5,3), columns=list('ABC'))
df.loc[operator.or_(df.C > 0.25, df.C < -0.25)]
A B C
0 1.764052 0.400157 0.978738
1 2.240893 1.867558 -0.977278
3 0.410599 0.144044 1.454274
4 0.761038 0.121675 0.4438
本站系公益性非盈利分享网址,本文来自用户投稿,不代表码文网立场,如若转载,请注明出处
评论列表(71条)