Data Science Asked on July 10, 2021
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 |
from scipy.stats.mstats import gmean
df['Geometric Mean'] = gmean(df.iloc[:,3])
df['Avg_Growth Mean'] = gmean(df.iloc[:,4])
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
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
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP