text_html_output_1.png
from sklearn.model_selection import train_test_split

from sklearn.preprocessing import OrdinalEncoder

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

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

missing_train = train.isna().sum().sum() + train.isnull().sum().sum()
missing_test = test.isna().sum().sum() + test.isnull().sum().sum()

from sklearn.model_selection import train_test_split
y = train['target']
X = train.drop(['target'], axis=1)
X_train, X_valid, y_train, y_valid = train_test_split(X, y, train_size=0.8, test_size=0.2, random_state=0)

object_cols = [col for col in X.columns if X[col].dtype == 'object']
good_label_cols = [col for col in object_cols if set(X_valid[col]).issubset(set(X_train[col]))]
bad_label_cols = list(set(object_cols) - set(good_label_cols))

from sklearn.preprocessing import OrdinalEncoder
enc = OrdinalEncoder()
label_X_train = X_train.drop(bad_label_cols, axis=1)
label_X_valid = X_valid.drop(bad_label_cols, axis=1)
label_X_train[good_label_cols] = enc.fit_transform(X_train[good_label_cols])
label_X_valid[good_label_cols] = enc.transform(X_valid[good_label_cols])

label_X_train.describe()