解决pandas:ValueError: Cannot convert non-finite values (NA or inf) to integer
Posted Data+Science+Insight
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了解决pandas:ValueError: Cannot convert non-finite values (NA or inf) to integer相关的知识,希望对你有一定的参考价值。
解决pandas:ValueError: Cannot convert non-finite values (NA or inf) to integer
目录
解决pandas:ValueError: Cannot convert non-finite values (NA or inf) to integer
问题:
分类的标签数据被客户错误地标注为了浮点型;
让学生去做出现了问题,有问题的程序如下;
df_in = df_origin
df_in['label'] = df_in['label'].astype("int")
#'Int64'
#df_in['label'] = df_in['label'].astype("Int64")
df_in['label'].value_counts()
# df_in['label'].describe()
解决:
初始数据类型为float64
转换时,正确的书写格式为Int64
这样转换可以转换过去,但是在进入模型的时候会发生问题,正确的的处理方式是:
缺失值填充后进行数据格式转换;
df_in = df_in.fillna(0)
df_in['label'] = df_in['label'].astype('int64')
df_in = df_origin
#df_in['label'] = df_in['label'].astype("int")
#'Int64'
df_in['label'] = df_in['label'].astype("Int64")
df_in['label'].value_counts()
# df_in['label'].describe()
完整错误:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-44-0f92eee9c8a0> in <module>
1 df_in = df_origin
----> 2 df_in['label'] = df_in['label'].astype("int")
3 # df_in['label'].value_counts()
4 # df_in['label'].describe()
D:\\anaconda\\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
D:\\anaconda\\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(
D:\\anaconda\\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:
D:\\anaconda\\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
D:\\anaconda\\lib\\site-packages\\pandas\\core\\dtypes\\cast.py in astype_nansafe(arr, dtype, copy, skipna)
1066
1067 if not np.isfinite(arr).all():
-> 1068 raise ValueError("Cannot convert non-finite values (NA or inf) to integer")
1069
1070 elif is_object_dtype(arr):
ValueError: Cannot convert non-finite values (NA or inf) to integer
以上是关于解决pandas:ValueError: Cannot convert non-finite values (NA or inf) to integer的主要内容,如果未能解决你的问题,请参考以下文章
pandas read_json时ValueError: Expected object or value的解决方案
Pandas:ValueError:传递值的形状是(110、10),索引暗示(111、10)
Pandas DataFrame:ValueError:值的长度(13)与索引的长度(12)不匹配
Pandas Dataframe ValueError:传递值的形状是(X,),索引暗示(X,Y)
pandas apply返回多列时出错ValueError: Must have equal len keys and value when setting with an iterable解决方案
pandas apply返回多列时出错ValueError: Must have equal len keys and value when setting with an iterable解决方案