The geometric mean applied to the row

I have this data frame as an example:

Col1 Col2 Col3 Col4 1 2 3 2.2 

I would like to add a 4th column called "Gmean", which calculates the geometric mean for the first 3 columns in each row.

How to do it?

Thanks!

+6
source share
2 answers

One way: Scipy 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 is

 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] . In addition, if you are looking for performance, you can work with the underlying array data using df.values or df.iloc[:,:3].values .

+5
source
 df.assign(Gmean=df.iloc[:, :3].prod(1) ** (1. / 3)) Col1 Col2 Col3 Col4 Gmean 0 1 2 3 2.2 1.817121 
+4
source

Source: https://habr.com/ru/post/1015244/


All Articles