class DataFrameImputer(TransformerMixin):

    def __init__(self):
        """Impute missing values.
        Columns of dtype object are imputed with the most frequent value 
        in column.
        Columns of other types are imputed with mean of column.
        """

def fit(self, X, y=None):
    self.fill = pd.Series([(X[c].value_counts().index[0] if X[c].dtype ==
        np.dtype('O') else X[c].mean()) for c in X], index=X.columns)
return self

def transform(self, X, y=None):
    return X.fillna(self.fill)
data = [['a', 1, 2], ['b', 1, 1], ['b', 2, 2], [np.nan, np.nan, np.nan]]
X = pd.DataFrame(data)
xt = DataFrameImputer().fit_transform(X)
print('before...')
print(X)
print('after...')
print(xt)
Impute categorical missing values in scikit-learn