Python-pandas:一个系列的真值不明确 [英] Python-pandas: the truth value of a series is ambiguous

查看:56
本文介绍了Python-pandas:一个系列的真值不明确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试将 json 文件(我已经可以处理)中的值与 csv 文件中的值(这可能是问题所在)进行比较.我当前的代码如下所示:

用于交易数据['timestamp']:数据 = pd.to_datetime(data)打印(数据)如果数据 == ask_minute['lastUpdated']:#....'做一点事'

给出:

<块引用>

":Series 的真值不明确.使用 a.empty、a.bool()、a.item()、a.any() 或 a.all()."

我当前的 print(data) 看起来像这样:

2018-10-03 18:03:38.0670002018-10-03 18:03:38.1090002018-10-03 18:04:282018-10-03 18:04:28.685000

但是,我仍然无法将 CSV 文件中的这些时间戳与 Json 文件中的时间戳进行比较.有人有想法吗?

解决方案

让我们将其简化为一个更简单的示例.例如,通过进行以下比较:

3 == pd.Series([3,2,4,1])0 真1 错误2 错误3 错误数据类型:布尔

您得到的结果是一个Series 布尔值,大小与表达式右侧的pd.Series 相等.所以这里真正发生的是整数在整个系列中广播,然后它们被比较.所以当你这样做时:

if 3 == pd.Series([3,2,4,1]):经过

<块引用>

ValueError:系列的真值不明确.使用 a.empty、a.bool()、a.item()、a.any() 或 a.all().

你得到一个错误.这里的问题是您将 pd.Series 与一个值进行比较,因此您将有多个 True 和多个 False 值,如在上述情况下.这当然是模棱两可的,因为条件既不是True 也不是False.

因此您需要进一步聚合结果,以便从操作中得到一个 boolean 值.为此,您必须使用 anyall 取决于您想要至少一个 (any) 或 all 值来满足条件.

(3 == pd.Series([3,2,4,1])).all()# 错误的

(3 == pd.Series([3,2,4,1])).any()# 真的

I am currently trying to compare values from a json file(on which I can already work on) to values from a csv file(which might be the issue). My current code looks like this:

for data in trades['timestamp']:
    data = pd.to_datetime(data)
    print(data)
       if data == ask_minute['lastUpdated']:
           #....'do something'

Which gives:

":The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()."

My current print(data) looks like this:

2018-10-03 18:03:38.067000
2018-10-03 18:03:38.109000
2018-10-03 18:04:28
2018-10-03 18:04:28.685000

However, I am still unable to compare these timestamps from my CSV file to those of my Json file. Does someone have an idea?

解决方案

Let's reduce it to a simpler example. By doing for instance the following comparison:

3 == pd.Series([3,2,4,1])

0     True
1    False
2    False
3    False
dtype: bool

The result you get is a Series of booleans, equal in size to the pd.Series in the right hand side of the expression. So really what's happening here is that the integer is being broadcast across the series, and then they are compared. So when you do:

if 3 == pd.Series([3,2,4,1]):
    pass

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

You get an error. The problem here is that you are comparing a pd.Series with a value, so you'll have multiple True and multiple False values, as in the case above. This of course is ambiguous, since the condition is neither True or False.

So you need to further aggregate the result so that a single boolean value results from the operation. For that you'll have to use either any or all depending on whether you want at least one (any) or all values to satisfy the condition.

(3 == pd.Series([3,2,4,1])).all()
# False

or

(3 == pd.Series([3,2,4,1])).any()
# True

这篇关于Python-pandas:一个系列的真值不明确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆