6
這是我的數據框:優化值的迭代計算基礎上的增長速度
Date A new_growth_rate
2011/01/01 100
2011/02/01 101
.
2012/01/01 120 0.035
2012/02/01 121 0.035
.
2013/01/01 131 0.036
2013/01/01 133 0.038
這就是我需要:
Date A new_growth_rate
2011/01/01 100
2011/02/01 101
.
.
2012/01/01 103.62 .035 A=100/(1-0.035)
2012/02/01 104.66 .035 A=101/(1-0.035)
.
.
2013/01/01 107.49 .036 A=103.62/(1-0.036)
2013/02/01 108.68 .038 A=104.66/(1-0.038)
我需要基於增長速度爲每列 來計算值我有一個400列的數據框和相應的增長率。
我已經使用以下公式計算增長率:(one year old value)*(1+current month growth rate)
。這個計算的值將用於獲取下一年的價值等等。像這樣我有400列和他們相應的增長率。時間序列具有30年的數據
當前我正在使用2 for循環一來獲取每列,然後第二遍迭代每列的時間段,並獲得前一個for循環計算的值。需要幾個小時才能查看500行和400列數據集。對此有一個更好的辦法`
我的代碼片段如下:?
grpby =在數據幀名單之列
df_new=pd.DataFrame()
for i,row in grpby.iterrows():
df_csr=grwth.loc[(grwth['A']==row['A'])].copy()
a = pd.to_datetime("2011-12-01",format='%Y-%m-%d')
b = a
while b <a+relativedelta.relativedelta(months=420):
b=b+relativedelta.relativedelta(months=1)
val= df_csr.loc[df_csr['Date']==(b+relativedelta.relativedelta(months=-12))].copy()
val2=val.get_value(val.index[0],'Val')
grwth_r=df_csr.loc[df_csr['date']==b]['new_growth_rate'].copy()
grwth_r2=grwth_r.get_value(grwth_r.index[0],'new_growth_rate')
df_csr.loc[df_csr['Date']==b,'Val']=val2/(1-grwth_r2)
df_new=pd.concat([df_new,df_csr])
請包括[MCVE(http://stackoverflow.com/help/mcve)(什麼是'grwth')給我們提供了足夠的數據一起玩,但沒有更多的 –
看對於series.rolling.apply –
GRWTH是列表 – Sanjay