首页 > 解决方案 > 如何检测 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”,但我无法确定它在哪一列中失败,并且由于我的多个列具有相似的值,为了处理此异常,我想知道它失败的列名。

标签: pandas

解决方案


我认为pdSchema是dict,因此您可以通过以下方式对其进行测试:

for k, v in pdSchema.items():
    try:
        result[k].astype(v)
    except ValueError:
        print (f'Column {k} failed for converting to {v}')
   

推荐阅读