Skip to main content
 首页 » 编程设计

python之具有增长率的 Pandas cumsum

2024年12月31日7lovecherry

我想计算 cumsum 但还要为每个总和添加增长率。

增长率 = 0.1

Cumsum with Growth 的每一行列将从前面的行中添加一个增长组件 cumsum:
Row 2 = (1+2) + (1*0.1)
Row 3 = (3.1 + 1) + (3.1 * 0.1)
Row 4 = (4.41 + 5) + (4.41* 0.1)

如何在不迭代 Pandas 中的每一行的情况下完成此操作?

请您参考如下方法:

你可以试试这个:

col = [df.val.values[0]] 
for i in range(1, len(df.index)): 
    col.append(col[i-1]+df.val.values[i]+col[i-1]*0.1) 
df['cumsumwithgrowth'] = col 

df

   val  cumsumwithgrowth 
0    1             1.000 
1    2             3.100 
2    1             4.410 
3    5             9.851