I know the year-on-year inflation rates for the past 5yrs. But I want to derive another column containing compounded inflation relative to the current year.
To illustrate, I have the below table where compound_inflation_to_2022 is the product of all yoy_inflation instances from each year prior to 2022.
So, for 2021 this is simply 2021's yoy_inflation rate. For 2020 the compound rate is 2020 x 2021. For 2019 the compound rate is 2019 x 2020 x 2021, and so on.
year | yoy_inflation | compound_inflation_to_2022 |
---|---|---|
2021 | 1.048 | 1.048 |
2020 | 1.008 | 1.056 |
2019 | 1.014 | 1.071 |
2018 | 1.02 | 1.093 |
2017 | 1.027 | 1.122 |
2016 | 1.018 | 1.142 |
Does anyone have an elegant solution for calculating this compound inflation column in python?
CodePudding user response:
So Pandas DataFrame has this feature called .cumprod() and I think it can be of utmost help to you.
df['compound_inflation_to_2022'] = df['yoy_inflation'].cumprod()
I hope this was what you were looking for ^_^