text_plain_output_1.png
from sklearn.compose import ColumnTransformer

from sklearn.impute import SimpleImputer

from sklearn.pipeline import Pipeline

from sklearn.preprocessing import OneHotEncoder

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

train = pd.read_csv('../input/30-days-of-ml/train.csv', index_col=0)
test = pd.read_csv('../input/30-days-of-ml/test.csv', index_col=0)

y = train['target']
features = train.drop(['target'], axis=1)

object_cols = [col for col in features.columns if 'cat' in col]
num_cols = [col for col in features.columns if features[col].dtype in ('int64', 'float64')]
X = features.copy()
X_test = test[features.columns].copy()

numerical_transformer = SimpleImputer(strategy='constant')
categorical_transformer = Pipeline(steps=[('imp', SimpleImputer(strategy='most_frequent')), ('OHen', OneHotEncoder(handle_unknown='ignore', sparse=False))])
processor = ColumnTransformer(transformers=[('num', numerical_transformer, num_cols), ('cat', categorical_transformer, object_cols)])
my_pipeline = Pipeline(steps=[('processor', processor)])
X_pre = pd.DataFrame(my_pipeline.fit_transform(X))
X_pre.index = X.index
X_test_pre = pd.DataFrame(my_pipeline.transform(X_test))
X_test_pre.index = X_test.index
print(X_pre, '\n', X_test_pre)