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.

 1|  from sklearn.impute import SimpleImputer
 2|  import numpy as np
 3|  
 4|  imputer = SimpleImputer(missing_values=np.nan, strategy='mean')
 5|  imputer.fit(df[column_name])
 6|  df[column_name] = imputer.transform(df[columns_name]))
2 Upvotes
Did you find this snippet useful?

Sign up to bookmark this in your snippet library

Normalize Windowed Time Series
Python
Data Preprocessing

Scaler | Normalize | Scale | Min-max

4
Pivoting Pandas Dataframes
Python
Data Preprocessing

Pandas

3