Is there a way in Pandas to use previous row value in dataframe.apply when previous value is also calculated in the apply?

The Solution to Is there a way in Pandas to use previous row value in dataframe.apply when previous value is also calculated in the apply? is


First, create the derived value:

df.loc[0, 'C'] = df.loc[0, 'D']

Then iterate through the remaining rows and fill the calculated values:

for i in range(1, len(df)):
    df.loc[i, 'C'] = df.loc[i-1, 'C'] * df.loc[i, 'A'] + df.loc[i, 'B']


  Index_Date   A   B    C    D
0 2015-01-31  10  10   10   10
1 2015-02-01   2   3   23   22
2 2015-02-02  10  60  290  280

~ Answered on 2016-01-18 14:09:23


Most Viewed Questions: