python中astype 存在空值_python-DataFrame.astype()错误参数
2023-06-13 09:11:45 时间
大家好,又见面了,我是你们的朋友全栈君。
使用列的字典时,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
相关文章
- pycharm自带python环境吗_Python IDE环境之 新版Pycharm安装详细教程[通俗易懂]
- python安装不了whl文件_Python安装whl文件过程图解
- 【说站】python查找计算函数的整理
- 【说站】python绘制散点图的两种方法
- python zipfile_Python 学习入门(16)—— zipfile
- [GSEAPY] 在Python里进行基因集富集分析
- Python金融时间序列模型ARIMA 和GARCH 在股票市场预测应用|附代码数据
- 【错误记录】PyCharm 运行 Python 程序报错 ( UnicodeDecodeError: ‘ascii‘ codec can‘t decode byte 0xe5 in positio )
- linux下python抓屏小程序详解编程语言
- 在目录下查找包含某字符串的文件的python代码详解编程语言
- Python学习:1.快速搭建python环境详解编程语言
- 安装python报0x80072f7d错误的最简单的解决方案,全程不足一分钟!!!详解编程语言
- Linux环境下安装Python(linux装python)
- python生成器的使用方法