Pandas 带增长率的累积总和
Pandas cumsum with growth rate
我想计算累积总和,但还要为每个总和加上增长率。
增长率= 0.1
Cumsum with Growth 列的每一行都会添加前几行的增长分量:
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 中的每一行的情况下完成此操作?
我将创建另一个名为 CumSumwithGrowth 的列
df['cum_sum'] = df.val1.cumsum()
df['CumSumWithGrowth'] = .10*df[‘cum_sum’] + df[‘cum_sum’]
你可以试试这个:
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
我想计算累积总和,但还要为每个总和加上增长率。
增长率= 0.1
Cumsum with Growth 列的每一行都会添加前几行的增长分量:
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 中的每一行的情况下完成此操作?
我将创建另一个名为 CumSumwithGrowth 的列
df['cum_sum'] = df.val1.cumsum()
df['CumSumWithGrowth'] = .10*df[‘cum_sum’] + df[‘cum_sum’]
你可以试试这个:
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