Skip to content

Instantly share code, notes, and snippets.

@alexlenail
Created May 25, 2022 17:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexlenail/1091fca1505053711cf9f2c09d12f05b to your computer and use it in GitHub Desktop.
Save alexlenail/1091fca1505053711cf9f2c09d12f05b to your computer and use it in GitHub Desktop.
from scipy.stats import gmean
def geometric_mean(df):
'''https://www.reddit.com/r/learnpython/comments/mq5ea7/pandas_calculate_geometric_mean_while_ignoring/'''
return df.replace(0, np.nan).apply(lambda row: gmean(row[~row.isna()]), axis=1).fillna(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment