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?