Pandas Apply for Sequential Row Value Calculations in Dataframes
When working with Pandas dataframes, you may encounter situations where you need to use the value of a previous row in a calculation. However, this can present challenges, especially when the previous value is also calculated within the same apply function.
Consider the following dataframe:
Index_Date A B C D ================================ 2015-01-31 10 10 Nan 10 2015-02-01 2 3 Nan 22 2015-02-02 10 60 Nan 280 2015-02-03 10 100 Nan 250
We want to create a new column, C, where:
Using an apply function and a shift with an if-else condition may not work due to key errors. Instead, we can follow these steps:
Step 1: Initialize Derived Value
First, we set the C value for the first row to be equal to D:
df.loc[0, 'C'] = df.loc[0, 'D']
Step 2: Iterate and Calculate
Next, we iterate through the remaining rows and calculate C using the previous row's C value:
for i in range(1, len(df)): df.loc[i, 'C'] = df.loc[i-1, 'C'] * df.loc[i, 'A'] + df.loc[i, 'B']
Result:
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 3 2015-02-03 10 100 3000 250
The above is the detailed content of How to Calculate Sequential Row Values in a Pandas DataFrame Using Apply and Shifting?. For more information, please follow other related articles on the PHP Chinese website!