Suppose I have a dataframe like the following
df = pd.DataFrame({'animal': ['Dog', 'Bird', 'Dog', 'Cat'],
'color': ['Black', 'Blue', 'Brown', 'Black'],
'age': [1, 10, 3, 6],
'pet': [1, 0, 1, 1],
'sex': ['m', 'm', 'f', 'f'],
'name': ['Rex', 'Gizmo', 'Suzy', 'Boo']})
I want to use label encoder to encode "animal", "color", "sex" and "name", but I don't need to encode the other two columns. I also want to be able to inverse_transform the columns afterwards.
I have tried the following, and although encoding works as I'd expect it to, reversing does not.
to_encode = ["animal", "color", "sex", "name"]
le = LabelEncoder()
for col in to_encode:
df[col] = fit_transform(df[col])
## to inverse:
for col in to_encode:
df[col] = inverse_transform(df[col])
The inverse_transform function results in the following dataframe:
animal | color | age | pet | sex | name |
---|---|---|---|---|---|
Rex | Boo | 1 | 1 | Gizmo | Rex |
Boo | Gizmo | 10 | 0 | Gizmo | Gizmo |
Rex | Rex | 3 | 1 | Boo | Suzy |
Gizmo | Boo | 6 | 1 | Boo | Boo |
It's obviously not right, but I'm not sure how else I'd accomplish this?
Any advice would be appreciated!