Home > Enterprise >  How I can fix key Error in OrdinalEncoder?
How I can fix key Error in OrdinalEncoder?

Time:06-15

enter image description here hello guys. Hi guys. As you can see in the picture, when I tried to use the target column, I encountered a key error. I tried different ways but it did not work. When I type: y= train['Level'] or y= train[['Level']] or print (Train['Level']) or enc = OrdinalEncoder() enc.fit(Train['Level'])

and any other things about Level column this Error occured. how I can fix it? please help me "| enter image description here

CodePudding user response:

When there is KeyError in dataframe you should always check for the columns by Train.columns because sometimes it may contain spaces or other characters that can not be seen by the eye so it would be good if you always checked for that.

I also want to add suggestions to your code, because you are trying to encode the target variable why don't use LabelEncoder instead of OrdinalEncoder?

the input for LabelEncoder is (n_samples,) that seems the case for you and the input for OrdinalEncoder is (n_samples, n_features). So I think you should use the first one.

Good Luck

  • Related