1

I have a dataframe as below, when i apply lambda it works fine but when i shift one value i am getting an error. I badly dont want to loop DF so wanted to shift value

Dataframe

def test(a,b,c):
    return a+b+c

    **this works fine**

    df['value'] = df.apply(lambda x: test(x['basic_ub'],x['basic_lb'],x['atr']), axis = 1)

    **when i shift, it give me error** 

    df['value'] = df.apply(lambda x: test(x['basic_ub'],x['basic_lb'],x['atr'].shift()), axis = 1)

    'numpy.float64' object has no attribute 'shift

'

Strike
  • 95
  • 1
  • 1
  • 8
  • What is your purpose in shifting? Or what, more clearly, is the goal of your code? It's not clear to me. – m13op22 Apr 18 '20 at 21:38
  • Does this answer your question? [Why can't I apply shift from within a pandas function?](https://stackoverflow.com/questions/28372847/why-cant-i-apply-shift-from-within-a-pandas-function) – mcskinner Apr 18 '20 at 21:41
  • It's a stock financial timeserise data. So I may have to calculate indicators based on previous values. Shift may differe from 1 to 14 and so on. Each data point in 5 minute. – Strike Apr 18 '20 at 22:37

1 Answers1

1

Because thé lambda function is taking thé value. There is probably à better way but you can solve it by creating a shifted column before:

x['atr_shifted' ]=x['atr'].shift()

df['value'] = df.apply(lambda x: test(x['basic_ub'],x['basic_lb'],x['atr_shifted' ]), axis = 1)
Renaud
  • 2,709
  • 2
  • 9
  • 24
  • Thanks Renuad, Is there a way I can perform the task without shifting the column before. The reason is I need to shift lot of column. The one I gave was an example . – Strike Apr 18 '20 at 22:34