0

I created a moving average using rolling on a dataframe using the following source code

    ma = produksi.price.rolling(window=3).mean()
    hasil = hasil.append(ma)
    mape= abs((produksi.price-ma)/produksi.price)

[![enter image description here][1]][1]

but the results of the moving average are like this, the calculation is correct, but the position is not as expected

I want the result like this [![enter image description here][2]][2]

to calculate the error value in the prediction of the fourth data [1]: https://i.stack.imgur.com/d8FBf.png [2]: https://i.stack.imgur.com/CalcT.png

  • 1
    Please provide your data in the text of your question, not as an image or link. See [How to make good pandas examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) – G. Anderson Feb 02 '21 at 19:50

1 Answers1

1

I think if you add the closed='left' parameter to rolling method you will get the desired outcome.

pandas rolling documentation

df = pd.DataFrame({'price': [3290, 3380, 3380, 3360, 3340, 3290, 3300]})
df['mean'] = df['price'].rolling(window=3, closed='left').mean()

Output

print(df)
   price         mean
0   3290          NaN
1   3380          NaN
2   3380          NaN
3   3360  3350.000000
4   3340  3373.333333
5   3290  3360.000000
6   3300  3330.000000
Timothy Helton
  • 332
  • 4
  • 10