Created
May 25, 2022 17:09
-
-
Save alexlenail/1091fca1505053711cf9f2c09d12f05b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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