小编Ral*_*ber的帖子

如何向量化 Pandas 中的累积运算

基于如何对使用先前值的操作进行向量化?,我无法回答以下问题:

有没有办法对期末值 (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 …
Run Code Online (Sandbox Code Playgroud)

python vectorization accumulate pandas

6
推荐指数
1
解决办法
292
查看次数

标签 统计

accumulate ×1

pandas ×1

python ×1

vectorization ×1