Home > database >  how to convert string to float in pandas dataframe
how to convert string to float in pandas dataframe

Time:02-21

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 :

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

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

CodePudding user response:

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)
                         )
  • Related