why cant i convert this to float
df_tmp['rain_1h'] = df_tmp['rain_1h'].replace({'zero': '0', }, regex=True)
df_tmp['rain_1h'] = df_tmp['rain_1h'].replace({"mm": ""}, regex=True)
df_tmp['rain_1h'] = df_tmp['rain_1h'].replace({" ": ""}, regex=True)
df_tmp['rain_1h'] = df_tmp['rain_1h'].str.strip().astype(float)
ValueError: could not convert string to float: ”
i already tried to delete the space manually, still error
>Solution :
In case of conversion and there are empty or non-numeric values in the ‘rain_1h’ column of your DataFrame that cannot be converted to float. To handle this issue, you can perform the following steps:
Replace any non-numeric values (e.g., ‘zero’, ‘mm’, and other non-numeric characters) with an empty string.
Replace any remaining empty strings with NaN (Not-a-Number).
Convert the column to float.
import pandas as pd
import numpy as np
# Sample
data = {'rain_1h': ['1.2 mm', 'zero', '3.4 mm', '']}
df_tmp = pd.DataFrame(data)
# Replace non-numeric characters with an empty string
df_tmp['rain_1h'] = df_tmp['rain_1h'].replace({'[^0-9.]': ''}, regex=True)
# Replace remaining empty strings with NaN
df_tmp['rain_1h'] = df_tmp['rain_1h'].replace({'': np.nan})
# Convert the column to float
df_tmp['rain_1h'] = df_tmp['rain_1h'].astype(float)
print(df_tmp)