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

Is it possible to do dynamic calculations for Pandas?

If I have a df/table let’s say

month     a     b
1         1     2
2         2     2
3         3     4
4         4     3
5         5     6
6         6     2

let’s say I want to only execute it from month 3 or above to get the desired output.

    month     a     b
    1         1     2
    2         2     2
    3         8     4
    4         24    3
    5         144   6
    6         288   2

The pattern above is take previous row of a and multiply it by current row b. Is it possible to do with without a for loop?

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 :

What you want is a cumulated product where the first value of b was replaced by that of a.

Using :

import numpy as np
df['a'] = np.cumprod(np.r_[df['a'].iloc[:1], df['b'].iloc[1:]])

With :

s = df['b'].copy()
s.iloc[0] = df['a'].iloc[0]

df['a'] = s.cumprod()

Output:

   month    a  b
0      1    1  2
1      2    2  2
2      3    8  4
3      4   24  3
4      5  144  6
5      6  288  2

partial assignment

start = 2 # third row
df['a'][start:] = np.cumprod(np.r_[df['a'].iloc[:1], df['b'].iloc[1:]])[start:]
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