2 Upvotes

Replacing NaNs in a Dataframe Column with Mean

Python
Data Preprocessing

Uses the simple imputer in Sklearn to replace the NaNs in a dataframe column with the mean of the column.

from sklearn.impute import SimpleImputer
import numpy as np

imputer = SimpleImputer(missing_values=np.nan, strategy='mean')
imputer.fit(df[column_name])
df[column_name] = imputer.transform(df[columns_name]))

By GregHe1979 - Last Updated Jan. 10, 2022, 10:52 p.m.

Did you find this snippet useful?

Sign up to bookmark this in your snippet library

COMMENTS
RELATED SNIPPETS
Pivoting Pandas Dataframes
Python
Data Preprocessing

Pandas

3
2
Top Contributors
98