TransWikia.com

How to apply Geomertic mean in python

Data Science Asked on July 10, 2021

  • 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?

2 Answers

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: Jupyternotebook screenshot with code

Answered by lytseeker on July 10, 2021

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

Answered by Mohtashim on July 10, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP