Simon
Simon

Reputation: 559

Pandas function calculation utilising previous row

Dataframe with one Input column. I'd like to add another Out column that multiplies the input row by 0.5 + (previous row Out value + 1). I've tried shift but think I am missing something basic.

Input dataframe:

df
Out[11]: 
   Input  
0     10  
1     20  
2     30  
3     20  
4     30  
5      0  
6     10  
7     20  

Output dataframe:

df
Out[11]: 
   Input  Out
0     10    5
1     20   16
2     30   32
3     20   43
4     30   59
5      0   60
6     10   66
7     20   77

Upvotes: 2

Views: 989

Answers (3)

Scott Boston
Scott Boston

Reputation: 153460

Here is a little trick to solve this problem:

import pandas as pd

df = pd.DataFrame({'input':[10,20,30,20,30,0,10,20]})

print(df)

Input dataframe:

   input
0     10
1     20
2     30
3     20
4     30
5      0
6     10
7     20

What you are really doing in this situation is running a variant of a cumulative sum, which we can model as follows:

df['out'] = ((df['input']/2).cumsum()) + (df['input'].notnull()).cumsum().add(-1)

Output results:

   input   out
0     10   5.0
1     20  16.0
2     30  32.0
3     20  43.0
4     30  59.0
5      0  60.0
6     10  66.0
7     20  77.0

Upvotes: 2

piRSquared
piRSquared

Reputation: 294258

df.assign(Out=df.Input.cumsum() / 2 + np.arange(len(df)))

   Input   Out
0     10   5.0
1     20  16.0
2     30  32.0
3     20  43.0
4     30  59.0
5      0  60.0
6     10  66.0
7     20  77.0

Upvotes: 2

boot-scootin
boot-scootin

Reputation: 12515

You're going to be missing a value for the 0th index, since there's no previous 'Out' value there.

df['Input'] * 0.5 + (df['Out'].shift(1) + 1)
Out[24]: 
0     NaN
1    16.0
2    32.0
3    43.0
4    59.0
5    60.0
6    66.0
7    77.0
dtype: float64

Upvotes: 1

Related Questions