# 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 & Herbs Ltd. 0.044881 0.000000 -0.773403
2010-11 Aayush Food & 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?
• What is your exact error? Dec 8 '20 at 19:31
• Error i am getting AttributeError: 'Series' object has no attribute 'gmean'
– HARI
Dec 9 '20 at 4:26

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))


As far as your error 'Series' object has no attribute 'gmean', make sure you are not doing this anywhere in your code:

# wrong way of calling gmean on a series
df.iloc[:,4].gmean() # accessing gmean as a function on series object



Instead how you have called gmean in your question description above is correct and works:

df['Avg_Growth Mean'] = gmean(df.iloc[:,4]) #works!


However, there might be more than one thing wrong here,

I tried testing code on data given by you and while gmean is available from the scipy.stats.mstats module, and it can be executed; the data is not apt for calculation of a geometric mean.

There is a zero value in Growth column, and negative values given in Avg Growth column. You can not find Geometric mean in case of negatives and zeroes: see this link for more details

See the image below for code I executed: