我有以下数据框,我必须计算今年和明年的预测。为此,我将过去4年的平均值乘以最近4个比率的平均值。
关键是我必须在今年和明年重复这个周期。 我得到了最终的价值,但是肯定有一种更简单的方法。
replaceState
因此,最终输出是:
import pandas as pd
import numpy as np
df = pd.DataFrame({"DATE":[2010,2011,2013,2014,2015,2016,2017,2018,2019,2020],"PROFIT":[40,50,60,75,80,95,110,120,np.nan,np.nan]})
df['PROFIT_LOAG'] = df['PROFIT'].shift(1)
df['RATIO'] = round(df['PROFIT']/df['PROFIT_LOAG'],2)
df['PROF_AVG'] = df['PROFIT'].rolling(window=4).mean()
df['RATIO_AVG'] = df['RATIO'].rolling(window=4).mean()
#Last 2 values to predict
for i in range(8,10):
df.loc[i,'PROFIT'] = df.loc[i-1,'PROF_AVG']*df.loc[i-1,'RATIO_AVG']
new_prof= df['PROFIT'].rolling(window=4).mean()
df.loc[i,'PROF_AVG'] = new_prof[i]
new_ratio=df.loc[i,'PROFIT']/df.loc[i-1,'PROFIT']
df.loc[i,'RATIO'] = new_ratio
new_ratio_avg = df['RATIO'].rolling(window=4).mean()
df.loc[i,'RATIO_AVG']=new_ratio_avg[i]