user5025141
user5025141

Reputation: 409

Define python apply function which can access previous row values and use them for calculation

I want to iterate through df and calculate the column a values which needs column b and c values. The formula is like this:

    for i in range(1, len(df)):
     df.a[i] = df.b[i-1]*(2**df.c[i])
     df.b[i] = df.a[i]*0.5 + 1

I want to have faster calculations maybe using apply func or lambda func, but I don't know when I define apply function in python, how can I access previous row's value?

Example input (df.a[0] df.b[0] df.c) and expected output df.a: df

a   b   c
0   1   1
4   3   2
24  13  3
208 105 4

Upvotes: 2

Views: 1191

Answers (1)

MaxU - stand with Ukraine
MaxU - stand with Ukraine

Reputation: 210852

you need shift() function:

In [9]: df = pd.DataFrame({'b': [1,3,13,105], 'c': [1,2,3,4]})

In [10]: df
Out[10]:
     b  c
0    1  1
1    3  2
2   13  3
3  105  4

In [11]: df['a'] = (df.b.shift()*(2**df.c)).fillna(0)

In [12]: df
Out[12]:
     b  c    a
0    1  1    0
1    3  2    4
2   13  3   24
3  105  4  208

In [13]: df.b = df.a*0.5+1

In [14]: df
Out[14]:
     b  c    a
0    1  1    0
1    3  2    4
2   13  3   24
3  105  4  208

In [15]: df[['a','b','c']]
Out[15]:
     a    b  c
0    0    1  1
1    4    3  2
2   24   13  3
3  208  105  4

Upvotes: 1

Related Questions