1

I am getting a The truth value of a Series is ambiguous error when I run the below function. Looking around the net it looks like it should work. Is there something I am missing? Also I am interested in how I could do this using list comprehension with the two columns in the loop.

I have tried using and and & get the some thing. I tried putting .any() but then it only uses the first statement. I looked at np.where but I need it to work with 3 if statement and I had no luck with that.

I see that others have asked this question the reason I ask again is that my code is the same as the answer from multiple if else conditions in pandas dataframe and derive multiple columns.

def before_after(df,col1, col2):
    if ((df[col1] >= 0) and (df[col2] >= 0)):
        return (((df[col2]-df[col1])/df[col1])*100)
    elif ((df[col1] < 0) and (df[col2] > 0)):
        return (((df[col2]-df[col1])/(df[col1].abs()))*100)
    elif ((df[col1] < 0) and (df[col2] < 0)):
        return (((df[col2]-df[col1])/df[col1])*100)

The error is:

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

Massifox
  • 4,369
  • 11
  • 31
Jeffkrop
  • 359
  • 1
  • 4
  • 14

1 Answers1

1

You can use numpy.select with chained first and third conditiond by | for bitwise OR, because same return, also if no condition is matched, is returned some default values, e.g. NaN:

def before_after(df,col1, col2):
    m1 = (df[col1] >= 0) & (df[col2] >= 0)
    m2 = (df[col1] < 0) & (df[col2] > 0)
    m3 = (df[col1] < 0) & (df[col2] < 0)

    s1 = ((df[col2]-df[col1])/df[col1])*100
    s2 = ((df[col2]-df[col1])/(df[col1].abs()))*100

    return np.select([m1 | m3, m2], [s1, s2], default=np.nan)    
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252