我想重现变量pv_exp_man:
import pandas as pd
df = pd.DataFrame({
'time' : [0,1,2,3,4,5],
'disc_fact' : [0.99,0.87,0.74,0.64,0.54,0.44],
'exp_man' : [0,100,95.45,93.11,87.46,81.77],
'pv_exp_man' : [204.211875, 206.274622, 137.097266, 89.816576, 47.228400, 0.000000]
})
公式为:
我可以循环,但我想要的是使用矢量化方法来计算它。这可能吗?
循环方法:
for k in range(len(df)-1,-1,-1):
if k == len(df)-1:
df.loc[df.time == k, 'pv_exp_man'] = 0.0
else:
df.loc[df.time == k, 'pv_exp_man'] = df.loc[df.time == k, 'disc_fact'].values[0] * (df.loc[df.time == k, 'exp_man'].values[0]+df.loc[df.time.isin([k+1]), 'pv_exp_man'].values[0])
这种类型的代码很难矢量化。但你可以加快计算速度,稍微重构一下代码:
out, df_rev = [], df[::-1]
for disc_fact, exp_man in zip(df_rev["disc_fact"], df_rev["exp_man"]):
if not out:
out.append(0)
else:
out.append((out[-1] + exp_man) * disc_fact)
df["out"] = out[::-1]
print(df)
打印:
time disc_fact exp_man pv_exp_man out
0 0 0.99 0.00 204.211875 204.211875
1 1 0.87 100.00 206.274622 206.274622
2 2 0.74 95.45 137.097266 137.097266
3 3 0.64 93.11 89.816576 89.816576
4 4 0.54 87.46 47.228400 47.228400
5 5 0.44 81.77 0.000000 0.000000