python中astype 存在空值_python-DataFrame.astype()错误参数
python,astype,存在,空值,DataFrame,错误,参数
2025-03-28 09:01:03 时间
大家好,又见面了,我是你们的朋友全栈君。
使用列的字典时,astype引发ValueError.
我试图将大DF中的稀疏列的类型转换(从float到int).我的问题是NaN值.即使将errors参数设置为’ignore’,使用列的字典时也不会忽略它们.
这是一个玩具示例:
t=pd.DataFrame([[1.01,2],[3.01, 10], [np.NaN,20]])
t.astype({0: int}, errors=’ignore’)
ValueError: Cannot convert non-finite values (NA or inf) to integer
解决方法:
您可以在pandas 0.24.0中使用新的nullable integer dtype.使用astype之前,您首先需要将不完全等于整数的所有浮点数转换为等于整数值(例如,舍入,截断等).
In [1]: import numpy as np; import pandas as pd; pd.__version__
Out[1]: ‘0.24.2’
In [2]: t = pd.DataFrame([[1.01, 2],[3.01, 10], [np.NaN, 20]])
In [3]: t.round().astype(‘Int64’)
Out[3]:
0 1
0 1 2
1 3 10
2 NaN 20
标签:pandas,python
来源: https://codeday.me/bug/20191210/2104644.html
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/141613.html原文链接:https://javaforall.cn
相关文章
- python 字符串基础练习
- Python模拟一个用户登录系统
- python 制作淘宝秒杀脚本
- 解决python中导出的pdf不能编辑问题
- 【玩转 Cloud Studio】使用Cloud Studio 进行python开发最佳实践
- Python 完美诠释"高内聚"概念的 IO 流 API 体系结构设计
- python中矩阵的转置怎么写_Python 矩阵转置的几种方法小结
- xgboost分类算法_python分类统计
- dataframe loc iloc_python的isnull函数
- 如何理解python报错信息_csb报错
- b站动漫_python爬b站视频
- 用Python画个生日蛋糕为朋友庆生
- Python-drf前戏38-前端Vue
- Python 让我舅舅的书法作品和 PIL 库发生点美的误会
- Python 随机(Random)模块的不可预测之美
- python dll注入 网络_dll注入
- 【Python】QQ查IP工具
- python测试框架unittest如何设置用例优先级_python 的 unittest 测试框架中的测试依赖怎么解决呢…[通俗易懂]
- 【水水水文章】用 Python 发邮件
- 如何在 Python 中使用断点调试