Getting TypeError: '<' not supported between instances of 'float' and 'str'



Experiment With data workshop

  • Missing Value Treatment

var_to_impute = [‘Workclass’,‘Occupation’,‘Native.Country’]

for var in var_to_impute:

Train[var].fillna(mode(Train[var]).mode[0], inplace=True)

TypeError: ‘<’ not supported between instances of ‘float’ and ‘str’


Since you’re calculating mode on a column with na/NaN values, the mode returned by the function will be of the type ‘float’, while the column values are of type ‘str’. Hence, the code is trying to insert a ‘float’ value in a column of ‘str’ values.
Preferably, when calling mode, drop the na rows and use that mode to replace the missing values.
Something like mode(Train[var].dropna(axis=0, how=‘any’))


Hello Mukul,

The error is due to possible NA/NAN in the column. You can take care of the NA in you code as follow:

Train[var].fillna(mode(Train[var], nan_policy=‘omit’)[0], inplace=True)

This uses scipy.stat mode function.


Hello sir,
its still having same error, do you know the another way to handle it?


yup, i had the same problem, i tried to debug the code and found out that it’s the ‘NaN’ values that are giving the trouble so during the time i first converted the columns that don’t have ‘NaN’ values then converted the whole;
in my case this worked i don’t know how biut try it in yours code too, it might work