Replacing the feature variables with encoded variables I have several nominal variables which I've encoded using the LabelEncoder() function. Now I want to replace the encoded values in the place of the raw datas of the features in the dataframe. I tried df.replace() but no luck. Below is the code snippet.

nominal_values = [
    'HouseStyle','ExterQual','BsmtQual','BsmtExposure','BsmtFinType1',
    'KitchenQual','GarageType','GarageFinish','GarageQual','GarageCond']


One of the column "KitchenQual" has values as follows:

{kitchenqual :['Ex','Gd','TA','Fa','Po']} representing "Excellent","Good","Typical/Average","Fair","Poor" respectively.

Another Feature "BsmtFinType1" has values as follows:

{BsmtFinType1: ['GLQ','ALQ','BLQ','Rec','LwQ','Unf','NA']} represeting

"Good Living Quarters","Average Liv. Quat.","Below Avg.Liv.Quat,","Avg. rec room", "Low Quality", "Unfinished","No Basement" respectively.

from sklearn.preprocessing import LabelEncoder

lbe = LabelEncoder()
for noms in nominal_values:
    encode = lbe.fit_transform(cat_var[noms])
    cat_var.replace(to_replace=cat_var[noms],
                    value=pd.Series(encode),inplace=True)


I used pd.Series(encode) since the replace function will support only one of the following data structures


Scalar
dict
Series
I don't know what is wrong with your code but this worked fine for me-

cat_var.replace(to_replace=cat_var[noms].tolist(),value=encode,inplace=True)

Instead of converting encoded values to series, convert series to list using tolist().