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
.
source share