Skip to content

Instantly share code, notes, and snippets.

@konverner
Created April 8, 2023 21:35
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 konverner/31eebda9d79ebda10bdc22133f5d2476 to your computer and use it in GitHub Desktop.
Save konverner/31eebda9d79ebda10bdc22133f5d2476 to your computer and use it in GitHub Desktop.
fillna based on some conditions
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