Created
April 8, 2023 21:35
-
-
Save konverner/31eebda9d79ebda10bdc22133f5d2476 to your computer and use it in GitHub Desktop.
fillna based on some conditions
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
column = ... | |
cond1, cond2 = ..., ... | |
agg_func = 'mean' | |
if agg_func == 'mean': | |
new_value = df[(cond1) & (cond2)][column].mean() | |
else: | |
new_value = df[(cond1) & (cond2)][column].mode()[0] | |
df.loc[(cond1) & (cond2), column] = df.loc[(cond1) & (cond2)][column].fillna(new_value) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment