使用逻辑表达式和if语句评估pandas系列值 [英] Evaluating pandas series values with logical expressions and if-statements

查看:2121
本文介绍了使用逻辑表达式和if语句评估pandas系列值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法使用if语句评估字典中的值。

I'm having trouble evaluating values from a dictionary using if statements.

给定以下字典,我从数据框导入(如果重要):

Given the following dictionary, which I imported from a dataframe (in case it matters):

>>> pnl[company]
29:   Active Credit       Date   Debit Strike Type
0      1      0 2013-01-08  2.3265  21.15  Put
1      0      0 2012-11-26      40     80  Put
2      0      0 2012-11-26     400     80  Put

我试图评估以下声明以建立最后一个值的值有效

I tried to evaluate the following statment to establish the value of the last value of Active:

if pnl[company].tail(1)['Active']==1:
    print 'yay'

但是,我遇到以下错误消息:

However,I was confronted by the following error message:

Traceback (most recent call last):
  File "<pyshell#69>", line 1, in <module>
    if pnl[company].tail(1)['Active']==1:
  File "/usr/lib/python2.7/dist-packages/pandas/core/generic.py", line 676, in __nonzero__
    .format(self.__class__.__name__))
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

这令我感到惊讶,因为我可以使用上面的命令显示我想要的值而没有if语句:

This surprised me, given that I could display the value I wanted using the above command without the if statement:

>>> pnl[company].tail(1)['Active']
30: 2    0
Name: Active, dtype: object

鉴于该值明显为零且索引为2,我尝试了以下内容进行简短的健全性检查,发现事情没有像我预期的那样发生:

Given that the value is clearly zero and the index is 2, I tried the following for a brief sanity check and found that things weren't happening as I might have expected:

>>> if pnl[company]['Active'][2]==0:
...     print 'woo-hoo'
... else:
...     print 'doh'


doh

我的问题是:

1)这里可能会发生什么?我怀疑我在一些基本层面上误解了字典。

1) What might be going on here? I suspect I'm misunderstanding dictionaries on some fundamental level.

2)我注意到,当我调出这个字典的任何给定值时,左边的数字增加了1.这代表什么?例如:

2) I noticed that as I bring up any given value of this dictionary, the number on the left increases by 1. What does this represent? For example:

>>> pnl[company].tail(1)['Active']
31: 2    0
Name: Active, dtype: object
>>> pnl[company].tail(1)['Active']
32: 2    0
Name: Active, dtype: object
>>> pnl[company].tail(1)['Active']
33: 2    0
Name: Active, dtype: object
>>> pnl[company].tail(1)['Active']
34: 2    0
Name: Active, dtype: object

提前感谢您提供任何帮助。

Thanks in advance for any help.

推荐答案

您的收益是熊猫系列对象并不能以您尝试的方式进行评估,即使它只是您需要将行更改为的单个值:

What you yield is a Pandas Series object and this cannot be evaluated in the manner you are attempting even though it is just a single value you need to change your line to:

if pnl[company].tail(1)['Active'].any()==1:
  print 'yay'

关于第二个问题,请参阅我的评论。

With respect to your second question see my comment.

编辑

从评论和链接到你的输出,调用 any()修复了错误信息,但你的数据实际上是字符串所以比较仍然失败,您可以这样做:

From the comments and link to your output, calling any() fixed the error message but your data is actually strings so the comparison still failed, you could either do:

if pnl[company].tail(1)['Active'].any()=='1':
  print 'yay'

要进行字符串比较,或修复数据,但它已被阅读或者生成。

To do a string comparison, or fix the data however it was read or generated.

或者执行:

pnl['Company']['Active'] = pnl['Company']['Active'].astype(int)

要转换列的 dtype ,以便您的比较更正确。

To convert the dtype of the column so that your comparison is more correct.

这篇关于使用逻辑表达式和if语句评估pandas系列值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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