How to perform cumulative division in python pandas

Viewed 107

I know that there would be more questions for cumulative division on stackoverflow. But my question is different from these questions.

Actually I want to do comulative division like we use comulative sum (cumsum) and comulative product(cumprod) in python. But there is no cumulative division function in python. If you have any alternative for this then tell me.

Problem:- Below is the dataframe (DF) in which I need to create Discount column and do cumulative division in (discount) column.

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)

Result:-

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

1 Answers

Try with 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

Discount[1] = Discount[0]/(1+Rates) = 1 / (1+Rates)^2

Related