I have a dataframe with lat long values, whose types should be float. However, for some rows, you can find things like -74.128815°, with the '°' character in the end of the string.
| id | Lat | Long |
|---|---|---|
| 1 | 4.807 | -75.684 |
| 2 | 4.5405 | -75.6658 |
| 3 | -74.128815° | |
| 4 | 5.35002 | -72.4002 |
| 5 | 4.6774° | -75.693 |
I want to keep all float values as they are but replace the values containing '°' (and then convert them to float), so that in the end I have this:
| id | Lat | Long |
|---|---|---|
| 1 | 4.807 | -75.684 |
| 2 | 4.5405 | -75.6658 |
| 3 | -74.128815 | |
| 4 | 5.35002 | -72.4002 |
| 5 | 4.6774 | -75.693 |
The DataFrame is named df. I have tried
df[df['Lat'].str.contains('°')]
which raises ValueError: Cannot mask with non-boolean array containing NA / NaN values error
Also, I've also tried df['Lat'] = np.where(df['Lat'].str.contains('°'), df['Lat'][:-1], df['Lat']) which raises ValueError: operands could not be broadcast together