首页 > 解决方案 > 如何在python pandas中执行累积除法

问题描述

我知道stackoverflow上的累积除法会有更多问题。但我的问题与这些问题不同。

实际上我想做计算除法,就像我们在 python 中使用计算总和(cumsum)和计算乘积(cumprod)一样。但是python中没有累积除法功能。如果您对此有任何选择,请告诉我。

问题:- 下面是我需要在其中创建折扣列并在(折扣)列中进行累积除法的数据框(DF)。

东风:-

Rates        Discount 
0.000028745455   1/(1+Rates)
0.000028745455   Discount[0]/(1+Rates)
0.000028745455   Discount[1]/(1+Rates)
0.000028745455   Discount[2]/(1+Rates)
0.000028745455   Discount[3]/(1+Rates)
0.000028745455   Discount[4]/(1+Rates)
0.000028745455   Discount[5]/(1+Rates)

结果:-

Rates              Discount 
0.000028745455   0.999971255
0.000028745455   0.999942512
0.000028745455   0.999913769
0.000028745455   0.999885026
0.000028745455   0.999856285
0.000028745455   0.999827545
0.000028745455   0.999798805

标签: pythonpandasnumpy

解决方案


尝试cumprod

df['out'] = (1/(df.Rates + 1)).cumprod()
Out[538]: 
0    0.999971
1    0.999943
2    0.999914
3    0.999885
4    0.999856
5    0.999828
6    0.999799
Name: Rates, dtype: float64

折扣[1] = 折扣[0]/(1+费率) = 1 / (1+费率)^2


推荐阅读