尝试对大熊猫进行一些预测。我有一个按月付款的收入明细表,并且想做一个总数的累积总和-我可以用.cumsum()
轻松地做到这一点。 不过我想添加一个5%pa的兴趣/增长因子。
示例df:
Month Payment
2018-01 1000
2018-02 2000
2018-03 500
2018-04 1000
2018-05 0
2018-06 0
我想输出:
Month Payment CumSum with Growth
2018-01 1000 1000
2018-02 2000 3005 (previous cumsum) * growth + 2000
2018-03 500 3510 (previous cumsum) * growth + 500
2018-04 1000 4510 (previous cumsum) * growth + 1000
2018-05 0 4515
2018-06 0 4520
答案 0 :(得分:0)
这里是一个快速尝试:
def interest(data, rate):
#turn yearly to monthly rate
monthly = rate/12.0
#hole output
out = np.zeros_like(data, dtype = float)
#initial deposit
out[0] = data[0]
for i in range(1, len(data)):
#t+1 = t*(1+monthly) + deposit
out[i] = out[i - 1]*(1+monthly)+ data[i]
return out
它应能按预期工作:
x = pd.DataFrame({'amount':[1000,2000,500, 1000,0,0]})
interest(x.amount, 0.05)
array([1000. , 3004.16666667, 3516.68402778, 4531.33687789,
4550.21744822, 4569.17668759])
答案 1 :(得分:-1)
假设增长常数:
growth = 0.05/12
df['aux'] = df['Payment']*(1+growth)
df.loc[0,'aux'] = df.loc[0,'Payment']
df['cumsum'] = df['aux'].cumsum()
df = df.drop('aux', axis = 1)
输出
Month Payment cumsum
0 2018-01 1000 1000.000000
1 2018-02 2000 3008.333333
2 2018-03 500 3510.416667
3 2018-04 1000 4514.583333
4 2018-05 0 4514.583333
5 2018-06 0 4514.583333