Ashok
Ashok

Reputation: 327

LabelEncoder is not converting the strings into numericals (0,1,2)

enter image description hereI have the data like below, I need to encode the variables, but LabelEncoder is not encoding the strings

My data looks like below

Delivery_class
First Class
Same Day
Second Class
Standard Class

X=filtered_df.iloc[:, 1]
labelencoder_X = LabelEncoder()
X.values[:,1] = labelencoder_X.fit_transform(X.values[:,1].astype(str))

even after running the abovr code the strings remains same.

Please advice, I am a beginner in XGBoost

Upvotes: 0

Views: 180

Answers (1)

Chris
Chris

Reputation: 29752

Don't assign back to X.values. Use X.iloc:

from sklearn.preprocessing import LabelEncoder
le = LabelEncoder()
X.iloc[:, 1] = le.fit_transform(X.values[:, 1].astype(str))

Output:

   Index  Ship_Mode
0      0          0
1      1          0
2      2          1
3      3          1
4      4          0
5      5          2

Upvotes: 1

Related Questions