Skip to content

Instantly share code, notes, and snippets.

@tanveer-sayyed
Created February 13, 2019 08:06
Show Gist options
  • Save tanveer-sayyed/ec6ab681a943d23bc06522306e304138 to your computer and use it in GitHub Desktop.
Save tanveer-sayyed/ec6ab681a943d23bc06522306e304138 to your computer and use it in GitHub Desktop.
In [62]:
# Usually .fillna() method is used to replace NaNs
new_df['c0'].fillna(mean_c0, inplace=True)
# We can do the same by .replace() method
new_df['c1'].replace(to_replace= np.nan, value= median_c1, inplace=True)
# Also by vectorised indexing
null_indexes_in_c3 = new_df.index[new_df['c3'].isnull()]
new_df.loc[null_indexes_in_c3, 'c3'] = mode_c3[0]
new_df
Out[62]:
c0 c1 c2 c3
index
i0 1.0 2.0 NaN 3.0
i1 5.5 7.0 NaN 10.0
i2 4.0 5.0 NaN 10.0
i3 6.0 7.0 NaN 8.0
i4 5.5 9.0 NaN 10.0
i5 11.0 12.0 NaN 10.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment