-1

I have a strange error occurring that I can't figure out.

Hoping for some help.

I have an if statement in my code as follows:

if my_df.ix[my_df['SOMEINTEGER'] == another_df.ix[i,'SOMECOMPARABLEINTEGER'],'SOMECOLUMN'] == 'I':

    *** DO SOMETHING ***
    i+=1

What this statement does is choose the row in my_df where 'SOMEINTEGER' equals the integer value in another_df.ix[i,'SOMECOMPARABLEINTEGER'] and check to see if the column value in 'SOMECOLUMN' in my_df is equal to 'I'

If I run the if statement as a single line of code with the value i set to an integer corresponding to a dataframe index value it works.

If I include this if statment in a larger iteration (and similiarly move the incrementing i out so it increments properly I get the following error:

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

I'm unable to isolate the source of the error.

Any help/insight appreciated.

Thanks in advance.

Windstorm1981
  • 2,564
  • 7
  • 29
  • 57
  • there can be several rows in my_df where 'SOMEINTEGER' equals the integer value in another_df.ix[i,'SOMECOMPARABLEINTEGER'], so you are comparing an array with a point. – alex314159 May 27 '17 at 20:12
  • What are you trying to do? It looks like your brackets are incorrect. – Andrew L May 27 '17 at 20:22

1 Answers1

0

The answer is contained in this post:

Evaluating pandas series values with logical expressions and if-statements

which, I guess, makes this a duplicate question (although I only coincidentally) stumbled upon this after doing a lot of searching.

Apparently the problem is a peculiarity of Pandas. The if statement structured with Pandas in this way produces a Pandas series object. The fix is to add .any() as:

if my_df.ix[my_df['SOMEINTEGER'] == another_df.ix[i,'SOMECOMPARABLEINTEGER'],'SOMECOLUMN'].any() == 'I':
Windstorm1981
  • 2,564
  • 7
  • 29
  • 57