我有一个带有一些nan的字符串系列,我想要替换一些字符,然后将其转换为int(float是可以的)但是nan仍然是nan。喜欢
In[1]:df = pd.DataFrame(["type 12", None, "type13"], columns=['A'])
Out[1]:
A
0 12
1 NaN
2 13
有没有好办法呢?
答案 0 :(得分:1)
不,不幸的是。你将不得不接受floats
。
>>> s = pd.Series(['1', '2', '3', '4', '5'], index=list('abcde'))
>>> s
a 1
b 2
c 3
d 4
e 5
dtype: object
>>> s = s.reindex(['a','b','c','f','u'])
>>> s
a 1
b 2
c 3
f NaN
u NaN
dtype: object
>>> s.astype(int)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/juan/anaconda3/lib/python3.5/site-packages/pandas/core/generic.py", line 2947, in astype
raise_on_error=raise_on_error, **kwargs)
File "/home/juan/anaconda3/lib/python3.5/site-packages/pandas/core/internals.py", line 2873, in astype
return self.apply('astype', dtype=dtype, **kwargs)
File "/home/juan/anaconda3/lib/python3.5/site-packages/pandas/core/internals.py", line 2832, in apply
applied = getattr(b, f)(**kwargs)
File "/home/juan/anaconda3/lib/python3.5/site-packages/pandas/core/internals.py", line 422, in astype
values=values, **kwargs)
File "/home/juan/anaconda3/lib/python3.5/site-packages/pandas/core/internals.py", line 465, in _astype
values = com._astype_nansafe(values.ravel(), dtype, copy=True)
File "/home/juan/anaconda3/lib/python3.5/site-packages/pandas/core/common.py", line 2628, in _astype_nansafe
return lib.astype_intsafe(arr.ravel(), dtype).reshape(arr.shape)
File "pandas/lib.pyx", line 937, in pandas.lib.astype_intsafe (pandas/lib.c:16620)
File "pandas/src/util.pxd", line 60, in util.set_value_at (pandas/lib.c:67979)
ValueError: cannot convert float NaN to integer
来自熊猫Caveats and Gotchas:
特殊值NaN(非-A-Number)在任何地方都用作NA 值,并且有API函数isnull和notnull可以 在dtypes中使用来检测NA值。
然而,它最后还有几个权衡取舍 肯定没有被忽视......在没有高性能NA的情况下 支持从头开始构建NumPy,主要 伤亡是指在整数数组中表示NA的能力。
这样做:
>>> s.astype(float)
a 1.0
b 2.0
c 3.0
f NaN
u NaN
dtype: float64