pandas - 如何检测 pandas 中的哪个 astype 标头转换失败?
问题描述
出于某种原因,我的大数据框无法进行 astype 转换,并且收到如下错误消息:
could not convert string to float: 'False'.
现在因为我有 100 列,我想检测 astype 转换在哪一列中失败,所以查看回溯:
result = result.astype(pdSchema)
File "C:\Users\spidey\AppData\Roaming\Python\Python39\site-packages\pandas\core\generic.py", line 5859, in astype
col.astype(dtype=dtype[col_name], copy=copy, errors=errors)
File "C:\Users\spidey\AppData\Roaming\Python\Python39\site-packages\pandas\core\generic.py", line 5874, in astype
new_data = self._mgr.astype(dtype=dtype, copy=copy, errors=errors)
File "C:\Users\spidey\AppData\Roaming\Python\Python39\site-packages\pandas\core\internals\managers.py", line 631, in astype
return self.apply("astype", dtype=dtype, copy=copy, errors=errors)
File "C:\Users\spidey\AppData\Roaming\Python\Python39\site-packages\pandas\core\internals\managers.py", line 427, in apply
applied = getattr(b, f)(**kwargs)
File "C:\Users\spidey\AppData\Roaming\Python\Python39\site-packages\pandas\core\internals\blocks.py", line 673, in astype
values = astype_nansafe(vals1d, dtype, copy=True)
File "C:\Users\spidey\AppData\Roaming\Python\Python39\site-packages\pandas\core\dtypes\cast.py", line 1097, in astype_nansafe
return arr.astype(dtype, copy=True)
ValueError: could not convert string to float: 'False'
我可以确定该值为“False”,但我无法确定它在哪一列中失败,并且由于我的多个列具有相似的值,为了处理此异常,我想知道它失败的列名。
解决方案
我认为pdSchema
是dict,因此您可以通过以下方式对其进行测试:
for k, v in pdSchema.items():
try:
result[k].astype(v)
except ValueError:
print (f'Column {k} failed for converting to {v}')
推荐阅读
- python - Python-Request - 尝试请求网站数据时收到指针错误
- npm - 带有 npm 的命令行参数可以消除对 `.npmrc` 文件的需要吗?
- r - 在 r 中 dcast 一个 data.table
- android - Scrollview中LinearLayout中的ImageView与CardStackView和毕加索图片不显示
- javascript - 在指定区域之外隐藏传单特征
- java - 如果某个属性出现多次,如何从 LinkedList 中删除项目?
- docker - 使用 docker 将图像、容器、文件、数据库迁移到另一台服务器
- javascript - 全局函数在一个代码中起作用,但在另一个代码中不起作用(未定义)
- javascript - 无法使用 jQuery 清除输入字段
- selenium - 如何从 CSS 选择器中选择第 n 个项目