python - ValueError:无法将字符串转换为浮点数:'86,5484466552734'
问题描述
当使用更改为浮点数时
final.iloc[:,4:10]=final.iloc[:,4:10].replace(',', '.').astype(float)
我收到以下错误:
[ValueError Traceback (most recent call last)
<ipython-input-33-56c93743e023> in <module>
1 final.dtypes
----> 2 final.iloc\[:,4:10\]=final.iloc\[:,4:10\].replace(',', '.').astype(float)
~\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pandas\core\generic.py in astype(self, dtype, copy, errors)
5875 else:
5876 # else, only a single dtype is given
-> 5877 new_data = self._mgr.astype(dtype=dtype, copy=copy, errors=errors)
5878 return self._constructor(new_data).__finalize__(self, method="astype")
5879
~\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pandas\core\internals\managers.py in astype(self, dtype, copy, errors)
629 self, dtype, copy: bool = False, errors: str = "raise"
630 ) -> "BlockManager":
--> 631 return self.apply("astype", dtype=dtype, copy=copy, errors=errors)
632
633 def convert(
~\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pandas\core\internals\managers.py in apply(self, f, align_keys, ignore_failures, **kwargs)
425 applied = b.apply(f, **kwargs)
426 else:
--> 427 applied = getattr(b, f)(**kwargs)
428 except (TypeError, NotImplementedError):
429 if not ignore_failures:
~\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pandas\core\internals\blocks.py in astype(self, dtype, copy, errors)
671 vals1d = values.ravel()
672 try:
--> 673 values = astype_nansafe(vals1d, dtype, copy=True)
674 except (ValueError, TypeError):
675 # e.g. astype_nansafe can fail on object-dtype of strings
~\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pandas\core\dtypes\cast.py in astype_nansafe(arr, dtype, copy, skipna)
1095 if copy or is_object_dtype(arr) or is_object_dtype(dtype):
1096 # Explicit copy, or required since NumPy can't view from / to object.
-> 1097 return arr.astype(dtype, copy=True)
1098
1099 return arr.view(dtype)
ValueError: could not convert string to float: '86,5484466552734'][1]
从中可以看出
ValueError:无法将字符串转换为浮点数:'86,5484466552734'
作为参考,这些是我正在使用的 df 列的数据类型(使用df.dtypes
)
Country.Name object
Country.Code object
Year int64
Prevalence of stunting, height for age (% of children under 5) object
Trained teachers in lower secondary education, male (% of male teachers) object
Share of youth not in education, employment or training, total (% of youth population) object
External debt stocks (% of GNI) object
Contributing family workers, male (% of male employment) (modeled ILO estimate) object
Tax revenue (% of GDP) object
Over-age students, primary, female (% of female enrollment) object
nans int64
dtype: object
解决方案
您正在尝试做的是替换,
with.
并且有多种方法可以做到这一点。
一种替代方法是使用apply
withstr.replace
考虑到您要将更改应用于final.iloc[:,4:10]
,以下可能会完成工作
final.iloc[:,4:10] = final.iloc[:,4:10].apply(lambda col: col.str.replace(',', '.').astype(float))
另一种是在读取文件时,例如 CSV,假设使用的是 pandas,需要显式传递decimals='.'
推荐阅读
- angular - 如何在模型中使用子组件在 Angular 6 中打开另一个组件?
- android - 在三星 note 8 上安装 apk 时出现“包解析错误”
- c# - C# - 每 5 分钟视频调用一次方法
- css - Safari 中的 SCSS 语法错误仅破解
- php - 使用变量插入时的 MySQLi 语法错误 (PHP)
- php - 如何使用 PHP 在 MySQL 中插入空间数据?
- dns - 显示我的确切站点的其他域?
- xamarin - 当我使用 XAML 时,它会继承颜色等属性吗?
- amazon-web-services - 至少一个安全组必须打开所有入口端口。AWS Glue 连接到 RDS
- c# - 如何在 C# 中的方法中返回多个值....有可能吗?