Skip to content

Instantly share code, notes, and snippets.

@GermanCM
Created February 2, 2019 18:36
Show Gist options
  • Save GermanCM/ba6097fb6eab9232bf5ebaaf2389a861 to your computer and use it in GitHub Desktop.
Save GermanCM/ba6097fb6eab9232bf5ebaaf2389a861 to your computer and use it in GitHub Desktop.
Impute attributes from a dataframe
def imputeMissingValues(dataframe, desired_strategy, attributes_to_impute):
from sklearn.preprocessing import Imputer
for attr in attributes_to_impute:
values_ = dataframe[attr].values.reshape(-1, 1)
imp = Imputer(missing_values=np.nan, strategy=desired_strategy, axis=0)
imp.fit(values_)
transformed_values = imp.transform(values_)
dataframe.loc[:,attr] = transformed_values
return dataframe
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment