Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Evaluating pandas series values with logical expressions and if-statements

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.

like image 776
neanderslob Avatar asked May 04 '14 20:05

neanderslob


People also ask

Can you use if statement in pandas?

As you work with values captured in pandas Series and DataFrames, you can use if-else statements and their logical structure to categorize and manipulate your data to reveal new insights.

How do you check if something is in a pandas Series?

isin() function check whether values are contained in Series. It returns a boolean Series showing whether each element in the Series matches an element in the passed sequence of values exactly.

How do you compare pandas Series values?

Compare two Series objects of the same length and return a Series where each element is True if the element in each Series is equal, False otherwise. Compare two DataFrame objects of the same shape and return a DataFrame where each element is True if the respective element in each DataFrame is equal, False otherwise.

Can ILOC be used in Series?

Purely integer-location based indexing for selection by position. . iloc[] is primarily integer position based (from 0 to length-1 of the axis), but may also be used with a conditional boolean Series.


1 Answers

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.

like image 183
EdChum Avatar answered Oct 18 '22 14:10

EdChum