Search code examples
pythonpandasfinance

Incorrect CAGR output using python in a Pandas dataframe


Apologies if this is an easy fix, but I can't figure out where my problem is - I am a relatively new programmer and have tried to find solutions elsewhere to no luck.

The issue: I am trying to calculate CAGR in a Pandas Dataframe, but the resultant metric does not match the calculation output in excel and also a third party check.

The Dataframe: simply a listing of countries (rows. Eg 'Afghanistan', 'Albania',..), and a listing of years (cols. Eg '1913', '1914'...) with GDP in the body of the table

The code:

    df_gdp['CAGR'] = ((df_gdp['2013']/df_gdp['1913'])**(1/(100)-1)*100)

The result:

I have added in a column at the end with the excel calculated results which show the differences. Indeed even with the first two rows (Afghanistan+Albania) the CAGR calc looks incorrect as it is clear Albania has grown more than Afghanistan

                1913            2013            CAGR        Excel
    country             
    Afghanistan 4,920,000,000   65,800,000,000  7.673647    2.627
    Albania     1,470,000,000   30,700,000,000  4.936023    3.086
    Algeria     22,600,000,000  479,000,000,000 4.864466    3.101
    Angola      3,230,000,000   152,000,000,000 2.208439    3.927
    

Solution

  • Problem was in () in formula:

    df_gdp['CAGR1'] = ((df_gdp['2013']/df_gdp['1913'])**(1/100)-1) * 100
    print (df_gdp)
                        1913          2013      CAGR  Excel     CAGR1
    Afghanistan   4920000000   65800000000  7.673647  2.627  2.627230
    Albania       1470000000   30700000000  4.936023  3.086  3.085649
    Algeria      22600000000  479000000000  4.864466  3.101  3.100856
    Angola        3230000000  152000000000  2.208439  3.927  3.926526