Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Geometric mean applied on row

I have this data frame as example:

Col1       Col2       Col3       Col4
   1          2          3        2.2

I would like to to add a 4th column called 'Gmean' that calculate the geometric mean of the first 3 columns on each row.

How can get it done ?

Thanks!

like image 702
datascana Avatar asked Feb 24 '17 10:02

datascana


2 Answers

One way would be with Scipy's geometric mean function -

from scipy.stats.mstats import gmean

df['Gmean'] = gmean(df.iloc[:,:3],axis=1)

Another way with the formula of geometric mean itself -

df['Gmean'] = np.power(df.iloc[:,:3].prod(axis=1),1.0/3)

If there are exactly 3 columns, just use df instead of df.iloc[:,:3]. Also, if you are looking for performance, you might want to work with the underlying array data with df.values or df.iloc[:,:3].values.

like image 51
Divakar Avatar answered Sep 18 '22 12:09

Divakar


df.assign(Gmean=df.iloc[:, :3].prod(1) ** (1. / 3))

   Col1  Col2  Col3  Col4     Gmean
0     1     2     3   2.2  1.817121
like image 36
piRSquared Avatar answered Sep 22 '22 12:09

piRSquared