Applying Calculations with Previous Row Values in Pandas
In Pandas, encountering the challenge of incorporating previous row values into calculations during data manipulation is not uncommon. One such scenario involves the need to use the previous row value when calculating a new column using the apply() function.
Consider a scenario where we have a DataFrame with the following structure:
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
Our goal is to populate the 'C' column with calculated values. For the first row, 'C' is derived from 'D'. For subsequent rows, 'C' is calculated by multiplying the previous row's 'C' value by the 'A' value for the current row and adding the 'B' value.
Approach
To achieve this, we employ a combination of initialization and iteration within the apply() function.
df.loc[0, 'C'] = df.loc[0, 'D']
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
This approach will effectively populate the 'C' column with the desired calculated values:
Index_Date A B C D ================================ 2015-01-31 10 10 10 10 2015-02-01 2 3 23 22 2015-02-02 10 60 290 280 2015-02-03 10 100 3000 250
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3