Geometric Mean Function in python pandas is used to calculate the geometric mean of a given set of numbers, Geometric mean of a data frame, Geometric mean of column and Geometric mean of rows. let’s see an example of each we need to use the package name “stats” from scipy in calculation of geometric mean. In this section we will learn,
- How to find the geometric mean of a given set of numbers
- How to find geometric mean of a dataframe
- How to find the geometric mean of a column in dataframe
- How to find row wise geometric mean of a dataframe
Geometric Mean Function in Python:
Simple geometric mean function is shown below
# calculate geometric mean from scipy import stats print(stats.gmean([1,9,5,6,6,7])) print(stats.gmean([4,11,15,16,5,7]))
output:
4.73989632394
8.47140270122
Geometric Mean of a dataframe:
Create dataframe
import pandas as pd import numpy as np from scipy import stats #Create a DataFrame d = { 'Name':['Alisa','Bobby','Cathrine','Madonna','Rocky','Sebastian','Jaqluine', 'Rahul','David','Andrew','Ajay','Teresa'], 'Score1':[62,47,55,74,31,77,85,63,42,32,71,57], 'Score2':[89,87,67,55,47,72,76,79,44,92,99,69]} df = pd.DataFrame(d) print df
So the resultant dataframe will be
Geometric Mean of the column in dataframe:
# Geometric Mean of the column in dataframe from scipy import stats scipy.stats.gmean(df.iloc[:,1:3],axis=0)
axis=0 argument calculates the column wise geometric mean of the dataframe so the result will be
Row wise geometric Mean of the dataframe:
# Row wise geometric mean of the dataframe from scipy import stats scipy.stats.gmean(df.iloc[:,1:3],axis=1)
axis=1 argument calculates the row wise geometric mean of the dataframe so the result will be
Calculate the geometric mean of the specific Column:
# geometric mean of the specific column scipy.stats.gmean(df.loc[:,"Score1"])
the above code calculates the geometric mean of the “Score1” column so the result will be