【发布时间】:2018-02-09 05:01:44
【问题描述】:
a = pd.Series([0.1,0.2,0.3,0.4])
>>> a.loc[[0,1,2]]
0 0.1
1 0.2
2 0.3
dtype: float64
当一个不存在的索引与现有索引一起添加到请求中时,它返回 NaN(这是我需要的)。
>>> a.loc[[0,1,2, 5]]
0 0.1
1 0.2
2 0.3
5 NaN
dtype: float64
但是当我只请求不存在的索引时,我得到了一个错误
>>> a.loc[[5]]
Traceback (most recent call last):
File "<pyshell#481>", line 1, in <module>
a.loc[[5]]
KeyError: 'None of [[5]] are in the [index]'
有没有办法再次获得 NaN 以避免诉诸 try/except 解决方案?
【问题讨论】:
-
stackoverflow.com/questions/37147735/… 我认为这应该会有所帮助
标签: python pandas indexing nan series