Python Forum

Full Version: Applying Moving Averages formula to a number
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have a csv file named data.


Within this file, I'd like to iterate over the rows and perform a moving averages calculation for the next 2 values based on the cell that contains the number 5.

I then would then like to print the calculation, with the phrase: 'Next 2 predictions for today: (calculation result)' and finally store this value in an array.


Output:
data: 9212020 9222020 9232020 9242020 9252020 5 3 2 1 1 0



This is what I am doing:

     import numpy as np
     import pandas as pd


     file = pd.read_csv("data.csv")


     predictions_array = []

            

     for row in file[1:]:
         value = float(row[1:4])
         predictions_array.append(value)
    


      formula = data['mov_avg'] = data['cum_sum'] / data['count']
      print('Next 2 predictions for today:', formula)


Desired output: (window of 5):

'Next 2 predictions for today: 2.8, 1.6 '

predictions_array = [2.8, 1.6]


I am a bit stuck within this process. Any help or suggestions is appreciated.
It is not clear to me what you want to do; however, you definitely need to look at .rolling method with a custom function (using .apply).