Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

could not convert string to float from pandas dataframe

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading