Home > database >  How to pass the value of previous row to the dataframe apply function?
How to pass the value of previous row to the dataframe apply function?

Time:08-24

I have the following pandas dataframe and would like to build a new column 'c' which is the summation of column 'b' value and column 'a' previous values. With shifting column 'a' it is possible to do so. However, I would like to know how I can pass the previous values of column 'a' in the apply() function.

l1 = [1,2,3,4,5]
l2 = [3,2,5,4,6]
df = pd.DataFrame(data=l1, columns=['a'])
df['b'] = l2
df['shifted'] = df['a'].shift(1)
df['c'] = df.apply(lambda row: row['shifted']  row['b'], axis=1)
print(df)

   a  b  shifted     c
0  1  3      NaN   NaN
1  2  2      1.0   3.0
2  3  5      2.0   7.0
3  4  4      3.0   7.0
4  5  6      4.0  10.0

I appreciate your help.

Edit: this is a dummy example. I need to use the apply function because I'm passing another function to it which uses previous rows of some columns and checks some condition.

CodePudding user response:

First let's make it clear that you do not need apply for this simple operation, so I'll consider it as a dummy example of a complex function.

Assuming non-duplicate indices, you can generate a shifted Series and reference it in apply using the name attribute:

s = df['a'].shift(1)

df['c'] =df.apply(lambda row: row['b'] s[row.name], axis=1)

output:

   a  b  shifted     c
0  1  3      NaN   NaN
1  2  2      1.0   3.0
2  3  5      2.0   7.0
3  4  4      3.0   7.0
4  5  6      4.0  10.0
  • Related