Python Forum
Applying Moving Averages formula to a number
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Applying Moving Averages formula to a number
#1
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.
Reply
#2
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).
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  gspread - applying ValueRenderOption to a range of cells dwassner 0 1,672 Jan-12-2022, 03:05 PM
Last Post: dwassner
  Python “Formula” Package: How do I parse Excel formula with a range of cells? JaneTan 1 2,641 Jul-12-2021, 11:09 AM
Last Post: jefsummers
  Applying function mapypy 1 2,236 Mar-11-2021, 09:49 PM
Last Post: nilamo
  Hi, I need help with defining user's input and applying it to code. jlmorenoc 2 2,237 Jun-24-2020, 02:10 PM
Last Post: pyzyx3qwerty
  Help with applying this instrument monitoring code to multiple inputs. Kid_Meier 1 2,043 Mar-04-2020, 12:01 PM
Last Post: Kid_Meier
  applying and(&) ,or(|) in conditions does not result output correctly as expected Smiling29 4 2,621 Oct-21-2019, 01:39 AM
Last Post: ichabod801
  Applying row height to all rows including and after row 7 curranjohn46 2 6,498 Oct-14-2019, 03:10 PM
Last Post: curranjohn46
  Calculating averages in Python NLittle17 1 2,355 Jan-13-2019, 02:54 PM
Last Post: ichabod801
  Perfect Number formula in Python Question an Mersenne Numbers Pleiades 5 5,970 May-16-2018, 04:56 PM
Last Post: Pleiades
  applying 2 conditions to a loop Pedroski55 1 2,806 Nov-08-2017, 07:11 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020