Pandas Calculate CAGR with Slicing
<p>Given the following data frame:</p>
<pre><code>df = pd.DataFrame({'A' : ['1','2','3','7'],
                       'B' : [7,6,5,4],
                       'C' : [5,6,7,1],
                       'D' : [1,9,9,8]})
df=df.set_index('A')
df
    B   C   D
A           
1   7   5   1
2   6   6   9
3   5   7   9
7   4   1   8
</code></pre>
<p>I am attempting to calculate the <a href="http://www.investopedia.com/terms/c/cagr.asp?layout=infini&amp;v=5A&amp;adtest=5A&amp;ato=3000" rel="nofollow noreferrer">compound annual growth rate (CAGR)</a>.
I am trying to avoid using the column names.
Here's what I came up with:</p>
<pre><code>df['CAGR']=((df[df.columns[-1:]]/df[df.columns[:1]])**(1/len(df.columns)))-1
</code></pre>
<p>However, it throws this error:</p>
<pre><code>ValueError: Wrong number of items passed 2, placement implies 1
</code></pre>
<p>I tested each part of the formula and it returned the columns I needed, so I'm stumped.</p>
<p>Thanks in advance!</p>