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

how to convert string to float in pandas dataframe

when I try the code:

clean_subdata['Tonn'] = clean_subdata['Tonn'].astype(float)

the output is :

ValueError: could not convert string to float: '2\xa0188.5'

than I tried :

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

clean_subdata['Tonn'] = clean_subdata['Tonn'].replace(r'\n','', regex=True)

But it’s doesn’t remove the whitespaces. can someone help?

>Solution :

If you want to remove all non digit or dot characters:

clean_subdata['Tonn'] = (clean_subdata['Tonn'].str.replace(r'[^\d.]',
                                                           '', regex=True)
                                              .astype(float)
                         )
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