栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Python

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Python

or
and
python语句需要
truth-
值。因为
pandas
这些被认为是模棱两可的,所以你应该使用
"bitwise" |(or)or&(and)
操作:

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

对于此类数据结构,它们会重载以产生元素级

or
(或
and
)。

只是为该语句添加更多解释:

当你想获取的时

bool
,会引发异常
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().

什么你打是一处经营隐含转换的操作数

bool
(你用
or
,但它也恰好为
and
if
while
):

>>> x or xValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().>>> x and xValueError: 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

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

有几个逻辑numpy的功能,它应该工作的pandas.Series。

如果你在执行if或时遇到异常,则异常中提到的替代方法更适合while。我将在下面简短地解释每个:

如果要检查你的系列是否为空:

>>> x = pd.Series([])>>> x.emptyTrue>>> x = pd.Series([1])>>> x.emptyFalse

如果没有明确的布尔值解释,

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

如果要检查所有或任何项目是否为非零,非空或非假:

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


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/374329.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号