I'm very new to Python, but am trying to do some basic automating of some company processes.
One of the processes is a basic forecast, where the numerical average growth is added to the last month's volume to get a forecast of the next month. This is then iterated over to get a longer term view.
I have already created:
- My dataframe
- My latest_row variable which pulls the last row in the data frame
- My numerical_avg_growth variable which calculates the numerical average growth
This is basically the process I want to create:
Calculate: latest_row['month'] + 1 (to get the next month number) latest_row['volume'] + numerical_avg_growth
Append the above calculations as a row to the main dataframe (and therefore this then becomes the latest_row variable)
And then repeat the process again
Meaning that you're constantly adding to the dataframe and calculating based on the most recent addition.
I can't figure out how to run the loop once, append that result, and then run the loop again based on that newly appended figure.
I'm sure this is fairly straightforward but I can't figure it out. Any help would be really appreciated!!
Data and Code Example:
#Creating dataframe
data = [[2022, 1, 512116, NaN], [2022, 2, 524775, -1.73],
[2022,3, 600017, 19.88]]
df = pd.DataFrame(data, columns=['year', 'month', 'volume',
'volume_growth'])
#Creating average volume growth percentage variable
avg_vol_growth = df['volume_growth'].mean()
#Creating average volume growth as integer variable
avg_vol_growth_int = np.sum(avg_vol_growth/100)
#Variable that shows just the last row in the dataframe
latest_row = df.tail(1)
#Creating numerical average growth variable (vol)
#Calculated using (latest actual volume * average growth integer)/12
numerical_avg_growth = np.sum(((latest_row['volume'])*avg_vol_growth_int)/12)
The result I need is, for example, to add the volume for months 4,5 and 6 to the dataframe. Calculated by adding the numerical_avg_growth to the volume of the previous month.
e.g year month volume 2022 1. 512116 2022. 2. 524775 2022. 3. 600017 2022. 4. (600017 + numerical_avg_growth) 2022. 5. (month 4 volume + numerical_avg_growth) 2022. 6. (month 5 volume + numerical_avg_growth)