我试图将大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...__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