【问题标题】:Why do i get this ValueError: could not convert string to float: '2.300\xa0%'?为什么我得到这个 ValueError: could not convert string to float: '2.300\xa0%'?
【发布时间】:2021-11-19 21:08:24
【问题描述】:
Taux moyen pondéré
2,300%
2,330%
3,134 %
....
df['Taux moyen pondéré']= df["Taux moyen pondéré"].str.replace(',', '.').astype(float)

【问题讨论】:

  • % 是不允许的。不知道为什么你还有\xa

标签: python pandas replace


【解决方案1】:

您只是将逗号 (,) 替换为点 (.) 在尝试将其转换为浮点数之前,您还必须删除百分比 (%) 符号。当字符串中包含 % 等特殊字符时,字符串无法转换为浮点数。

【讨论】:

  • 如何去掉 % 符号?
  • df['Taux moyen potéré']= df["Taux moyen potéré"].str.replace("%", "")
  • 谢谢。它有效
  • 不客气。随意点赞! :)
猜你喜欢
  • 2020-12-11
  • 1970-01-01
  • 2019-12-18
  • 2018-02-13
  • 1970-01-01
  • 2021-11-26
  • 2019-11-10
  • 2019-05-22
  • 1970-01-01
相关资源
最近更新 更多