SNIPPET
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 Dec. 12, 2020, 11:29 p.m.

COMMENTS
RELATED SNIPPETS
Pivoting Pandas Dataframes
Python
Data Preprocessing

Pandas

3
SparkSession instantiate
Python
Data Preprocessing

2
Converting Data Types
Python
Data Preprocessing

Data-cleaning

2
Data Minification
Python
Data Preprocessing

Cleaning

2
Search Snippets by Tag: