Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Create a column where the values are caculated on previously caculated values

I am wondering if there’s a way to do multiplication with a previously derived value in the newly created column.

import pandas as pd
df = {1: {0: 100.0, 1: 0.96, 2: 0.93, 3: 0.88, 4: 0.85, 5: 0.8}}

        1
0  100.00
1    0.96
2    0.93
3    0.88
4    0.85
5    0.80

Logic:
1) 1 = 1
2) 0.96 * 1 (previously derived value) = 0.96
3) 0.93 * (0.96) (previously derived value) = 0.8928
4) 0.88 * (0.8928) (previously derived value) = 0.785664

Expected output:

        1           2
0     1.0         1.0
1    0.96        0.96
2    0.93      0.8928
3    0.88    0.785664
4    0.85   0.6678144
5    0.80  0.53425152

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You want a cumprod, divided by the first value:

df[2] = df[1].cumprod().div(df[1].iloc[0])

Output:


        1         2
0  100.00  1.000000
1    0.96  0.960000
2    0.93  0.892800
3    0.88  0.785664
4    0.85  0.667814
5    0.80  0.534252
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading