根据如何对使用前一个值的操作进行矢量化的回答?,我无法回答以下问题:
是否有一种方法可以矢量化“期末值”(VEoP)列?
import pandas as pd
terms = pd.date_range(start = '2022-01-01', periods=12, freq='YS', normalize=True)
df = pd.DataFrame({
'Return': [1.063, 1.053, 1.008, 0.98, 1.04, 1.057, 1.073, 1.027, 1.025, 1.068, 1.001, 0.983],
'Cashflow': [6, 0, 0, 8, -1, -1, -1, -1, -1, -1, -1, -1]
},index=terms.strftime('%Y'))
df.index.name = 'Date'
df['VEoP'] = 0
for y in range(0, df.index.size):
df['VEoP'].iloc[y] = ((0 if y==0 else df['VEoP'].iloc[y-1]) + df['Cashflow'].iloc[y]) * df['Return'].iloc[y]
df
Return Cashflow VEoP
Date
2022 1.0630 6 6.3780
2023 1.0530 0 6.7160
2024 1.0080 0 6.7698
2025 0.9800 8 14.4744
2026 1.0400 -1 14.0133
2027 1.0570 -1 13.7551
2028 1.0730 -1 13.6862
2029 1.0270 -1 13.0288
2030 1.0250 -1 12.3295
2031 1.0680 -1 12.0999
2032 1.0010 -1 11.1110
2033 0.9830 -1 9.9391