Home > Blockchain >  How do I create a an annualized growth rate for GDP column in my dataframe?
How do I create a an annualized growth rate for GDP column in my dataframe?

Time:06-27

I basically want to apply this formula: ((New/Old)^4 - 1) * 100. To a data frame I have and create a new column called "Annualized Growth Rate" I have been working off of the FRED GDP data set.

I have a data set that looks something like this (not the real numbers)

Index GDP
0 100
1 101
2 103
3 107

I want to add a column on the right that applies the formula I mentioned above so that the annualized growth rate numbers would start appearing in the index = 1 position

Index GDP Annualized_Growth_Rate
0 100 NaN
1 101 4.060
2 103 8.159
3 107 16.462

How can I go about doing this. I was originally trying to do it using iterrows() Something like

for index,row in iterrows():


   df['Annualized Growth Rate'] = df['GDP'].loc[index] / df.['GDP'].loc[index - 1]...

but then index position -1 is out of range.

I'm assuming there is an easy way to go about this that I just don't know. I also know you aren't really supposed to use something like iterrows. Any guidance would be appreciated.

CodePudding user response:

You can use shift to access the previous row and vectorial operations for subtraction, division, power, multiplication:

df['Annualized_Growth_Rate'] = df['GDP'].div(df['GDP'].shift()).pow(4).sub(1).mul(100)

Output:

   Index  GDP  Annualized_Growth_Rate
0      0  100                     NaN
1      1  101                4.060401
2      2  103                8.159184
3      3  107               16.462528
  • Related