how to fix this error "Encoders require their input to be uniformly strings or numbers. Got ['float', 'str']"

Viewed 3641

Here is my code.

label_encoder = LabelEncoder()

rainfall['station_name'] = label_encoder.fit_transform(rainfall['station_name'])
2 Answers

It's possible that you have empty values within the columns. This can be checked with:

rainfall.isnull().any()

If you quickly want to get rid of any and all rows missing values you can use:

rainfall= rainfall.dropna()

If you only want to remove rows where the station_name is missing use:

rainfall.dropna(subset=['rainfall'])
label_encoder = LabelEncoder()

In order to resolve this, you need to completely convert your feature i.e., station_name into an str data type.

rainfall['station_name'] = rainfall['station_name'].astype(str)

Then encode the feature:

rainfall['station_name'] = label_encoder.fit_transform(rainfall['station_name'])
Related