In my dataframe 'data' I have two columns 'trend' & 'rtrend'
trend has values -1, 0 and 1.
def newfunc(a):
j = -1
for i in a:
j = j+1
x = (j-1)
if data.iloc[j]['trend'] != 0:
return data.iloc[j]['trend']
if data.iloc[j]['trend'] == 0:
return data.iloc[x]['rtrend']
If trend is equal to -1 or 1 then I'd like to set the rtrend column value equal to trend.
If trend equals 0, then set rtrend equal to the last value in that series which appears above in the dataframe.
data['rtrend'] = newfunc(data['trend'])
All it currently returns is 0 for the whole series.
Please could someone point me in the right direction? I'm sure there must be a better way to do this. (I've tried np.where() which doesn't seem to do what I'm after).