text_plain_output_1.png
from sklearn.preprocessing import OneHotEncoder

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

df_train = pd.read_csv('/kaggle/input/30-days-of-ml/train.csv')
df_test = pd.read_csv('/kaggle/input/30-days-of-ml/test.csv')

cat_columns = [col for col in df_train.columns if df_train[col].dtype == 'object']

low_cordinal_cols = [col for col in cat_columns if df_train[col].nunique() < 10]
low_cordinal_cols

OH_encoder = OneHotEncoder(handle_unknown='ignore', sparse=False)
OH_encoder_Train = pd.DataFrame(OH_encoder.fit_transform(X_train[low_cordinal_cols]))
OH_encoder_valid = pd.DataFrame(OH_encoder.transform(X_valid[low_cordinal_cols]))

OH_encoder_Train.index = X_train.index
OH_encoder_valid.index = X_valid.index

OH_encoder_valid.head()