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().
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
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
My Question is:
1) What might be going on here? I suspect I'm misunderstanding dictionaries on some fundamental level.
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.
EDIT
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.
Or do:
pnl['Company']['Active'] = pnl['Company']['Active'].astype(int)
To convert the dtype
of the column so that your comparison is more correct.