Reputation: 1629
What's the problem with this code? I used many comparison lambda function on the dataframe,but this one returns ValueError: ('The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().', u'occurred at index 2')
error.
I searched about it and found many question asked before about it,but none of them fit my problem.
My code:
def Return(close,pClose):
i = ((close - pClose) / close) * 100
if (i > 0):
return 1
if (i < 0):
return 0
df['return'] = df.apply(lambda y:Return(close=df['Close'], pClose=df['pClose']),axis=1)
Upvotes: 0
Views: 2925
Reputation: 416
The Problem with your code is that you pass the whole column of the dataframe to your function:
df.apply(lambda y:Return(close=df['Close'], pClose=df['pClose']),axis=1)
In the function you are calculating a new value i which is in fact a column:
i = ((close - pClose) / close) * 100
In the comparison statement thencannot decide how to evaluate what you are trying to do because it gets a column as input:
if (i > 0):
So I think what you want is something like:
df['return'] = df.apply(lambda y:Return(close=y['Close'], pClose=y['pClose']),axis=1)
Upvotes: 3