text_html_output_1.png
from sklearn.impute import SimpleImputer

import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

train = pd.read_csv('/kaggle/input/home-data-for-ml-course/train.csv')
test = pd.read_csv('/kaggle/input/home-data-for-ml-course/train.csv')

numerical_cols = [col for col in X_train_full.columns if X_train_full[col].dtype == 'int64' or X_train_full[col].dtype == 'float64']

categorical_cols_prev = [col for col in X_train_full.columns if X_train_full[col].dtype == 'object' and X_train_full[col].nunique() > 4]
categorical_cols = []
for col in categorical_cols_prev:
    if set(list(test[col].unique())).issubset(set(list(X_train_full[col].unique()))):
        categorical_cols.append(col)

X_train = X_train_full[numerical_cols + categorical_cols]
X_valid = X_valid_full[numerical_cols + categorical_cols]
X_test = test[numerical_cols + categorical_cols]

from sklearn.impute import SimpleImputer
impute = SimpleImputer(strategy='mean')
X_train[numerical_cols] = impute.fit_transform(X_train[numerical_cols])
X_valid[numerical_cols] = impute.transform(X_valid[numerical_cols])
X_test[numerical_cols] = impute.transform(X_test[numerical_cols])