0

EDITED:

I am trying to calculate the values inside a data frame column in pandas, using the same column and the previous one. I start with this data frame:

    foo     bar
0   0.51    NaN
1   0.25    NaN
2   0.73    NaN
3   0.08    NaN
4   0.43    0.4
5   0.39    NaN
6   0.15    NaN
7   0.02    NaN
8   0.76    NaN
9   0.41    NaN

I need to smooth the values of foo in the following way, starting from the row 5 and onwards:

bar = (bar(previous row) * 4 + foo) / 5

And it should result in the following:

    foo     bar
0   0.51    NaN
1   0.25    NaN
2   0.73    NaN
3   0.08    NaN
4   0.43    0.4
5   0.39    0.398
6   0.15    0.3484
7   0.02    0.28272
8   0.76    0.378176
9   0.41    0.3845408

I tried to use df.loc[5:,'bar'] = (df.loc[:,'bar'].shift() * 4 + df.loc[:,'foo']) / 5, but the result is the following:

    foo    bar
0  0.51    NaN
1  0.25    NaN
2  0.73    NaN
3  0.08    NaN
4  0.43    0.4
5  0.39  0.398
6  0.15    NaN
7  0.02    NaN
8  0.76    NaN
9  0.41    NaN

I can imagine that an instance of df is created and the new 'bar' is created from that instance, instead of doing it in a recursive way. Can anyone help with this, without using a for loop, please? Thanks

Paco2074
  • 41
  • 5
  • 1
    What is output if in `bar` in first value 3? – jezrael Dec 08 '17 at 20:44
  • Hi jezrael, I edited the question, I before it was formulated in a way it was misleading. Actually, what I apply it is not a cumulative sum, but a smoothing function. If you think this is not a duplicate from a previous question, I would appreciate if you could untag it as [duplicate]. Thanks. – Paco2074 Dec 08 '17 at 21:19

0 Answers0