如何将字符串转换为 pandas 数据框中的浮点数

how to convert string to float in pandas dataframe

当我尝试代码时:

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

输出是:

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

比我试过:

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

但它不会删除空格。有人可以帮忙吗?

如果要删除所有非数字或点字符:

clean_subdata['Tonn'] = (clean_subdata['Tonn'].str.replace(r'[^\d.]',
                                                           '', regex=True)
                                              .astype(float)
                         )