How to apply Geomertic mean in python I am trying to built a function to calculate the Geometric mean for Growth column and Avg_growth.





Year
Company Name
FH_PBIDT
Growth
Avg_growth




2009-10
Aayush Food &amp; Herbs Ltd.
0.044881
0.000000
-0.773403


2010-11
Aayush Food &amp; Herbs Ltd.
0.063545
0.415839
-0.773403





For that, I am using the below code:

from scipy.stats.mstats import gmean

I created a column name:

df['Geometric Mean'] = gmean(df.iloc[:,3])
df['Avg_Growth Mean'] = gmean(df.iloc[:,4])

I am getting an error that gmean is not defined. Could someone help me here?
Can also write a geometric mean function by a little help  from Numpy.
import numpy as np

def geometric_mean(i):
    x = np.array(i)
    return x.prod()**(1.0/len(x))