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().