numpy column wise division to the power of n

55 Views Asked by At

I have a pandas dataframe in python and I want to generate RevenueCAGR column,

Company Name                zumba pty ltd
Total Revenue;2018-06-30          17102.2
Total Revenue;2016-06-30           2111.5
RevenueCAGR                   4.22897e+16


x2 = {'Total Revenue;2018-06-30' : [17012.2],
                'Total Revenue;2016-06-30' : [2111.5],
                'RevenueCAGR' : [422897]}

x2df = pd.DataFrame.from_dict(x2)

I am trying to get (Total Revenue;2018-06-30/Total Revenue;2016-06-30)^(1/3)-1.

I tried

df['RevenueCAGR'] = np.power(df['Total Revenue;2018-06-30']/df['Total Revenue;2016-06-30'], 1/3) -1

And I got this error:

__main__:1: RuntimeWarning: invalid value encountered in power

How do you get column wise operations in python to take to the power of a float?

1

There are 1 best solutions below

0
jpp On

If your dataframe is structured correctly, your code will work:

df = pd.DataFrame({'TR_2018': [17102.2],
                   'TR_2016': [2111.5]})

df['RevenueCAGR'] =  np.power(df['TR_2018']/df['TR_2016'], 1/3) -1

print(df)

   TR_2016  TR_2018  RevenueCAGR
0   2111.5  17102.2     1.008262