close

[Solved] LabelEncoder: TypeError: ‘>’ not supported between instances of ‘float’ and ‘str’

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error LabelEncoder: TypeError: ‘>’ not supported between instances of ‘float’ and ‘str’ in python. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How LabelEncoder: TypeError: ‘>’ not supported between instances of ‘float’ and ‘str’ Error Occurs?

Today I get the following error LabelEncoder: TypeError: ‘>’ not supported between instances of ‘float’ and ‘str’ in python.

How To Solve LabelEncoder: TypeError: ‘>’ not supported between instances of ‘float’ and ‘str’ Error ?

  1. How To Solve LabelEncoder: TypeError: '>' not supported between instances of 'float' and 'str' Error ?

    To Solve LabelEncoder: TypeError: '>' not supported between instances of 'float' and 'str' Error As string data types have variable length, it is by default stored as object type. I faced this problem after treating missing values too.

  2. LabelEncoder: TypeError: '>' not supported between instances of 'float' and 'str'

    To Solve LabelEncoder: TypeError: '>' not supported between instances of 'float' and 'str' Error As string data types have variable length, it is by default stored as object type. I faced this problem after treating missing values too.

Solution 1

This is due to the series df[cat] containing elements that have varying data types e.g.(strings and/or floats). This could be due to the way the data is read, i.e. numbers are read as float and text as strings or the datatype was float and changed after the fillna operation.

In other words

pandas data type ‘Object’ indicates mixed types rather than str type

so using the following line:

df[cat] = le.fit_transform(df[cat].astype(str))


should help

Solution 2

As string data types have variable length, it is by default stored as object type. I faced this problem after treating missing values too. Converting all those columns to type ‘category’ before label encoding worked in my case.

df[cat]=df[cat].astype('category')

And then check df.dtypes and perform label encoding.

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read