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

Here is my code.

label_encoder = LabelEncoder()

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


Solution 1:[1]

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'])

Solution 2:[2]

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'])

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Mitchell Appelman
Solution 2 Suraj Rao