text_plain_output_1.png
from sklearn.preprocessing import LabelEncoder
def encode_categorical(train_df, test_df, categ_feat=categorical_features):
    """
    """
    encoder_dict = {}
    concat_data = pd.concat([trn_data[categ_feat], tst_data[categ_feat]])
    for col in concat_data.columns:
        print('Encoding: ', col, '...')
        encoder = LabelEncoder()
        encoder.fit(concat_data[col])
        encoder_dict[col] = encoder
        train_df[col + '_Enc'] = encoder.transform(train_df[col])
        test_df[col + '_Enc'] = encoder.transform(test_df[col])
    train_df = train_df.drop(columns=categ_feat, axis=1)
    test_df = test_df.drop(columns=categ_feat, axis=1)
    return (train_df, test_df)