I’m applying a LabelEncoder to a pandas DataFrame, df
Feat1 Feat2 Feat3 Feat4 Feat5
A A A A E
B B C C E
C D C C E
D A C D E
I'm applying a label encoder to a dataframe like this -
from sklearn import preprocessing
le = preprocessing.LabelEncoder()
intIndexed = df.apply(le.fit_transform)
This is how the labels are mapped
A = 0
B = 1
C = 2
D = 3
E = 0
I'm guessing that E
isn't given the value of 4
as it doesn't appear in any other column other than Feat 5
.
I want E
to be given the value of 4
- but don't know how to do this in a DataFrame.
You could
fit
the label encoder and latertransform
the labels to their normalized encoding as follows:One way to specify labels by default would be:
You can fit and transform in single statement, Please find the code for encoding single column and assigning back to data frame.