Home > Back-end >  Is it possible to do dynamic calculations for Pandas?
Is it possible to do dynamic calculations for Pandas?

Time:10-15

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?

CodePudding user response:

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:]
  • Related