简体   繁体   English

使用列表为DataFrame编制索引,并为熊猫的越界索引返回NaN?

[英]Index a DataFrame with a list and return NaN for out-of-bounds indices in Pandas?

Normally when I index a DataFrame (or a Series) with a list of integer indices, I get back a subset of the rows, unless some of my indices are out of bounds, in which case I get an IndexError : 通常,当我使用整数索引列表对DataFrame(或Series)进行索引时,我会返回行的子集,除非我的某些索引超出范围,在这种情况下,我会得到IndexError

s = pd.Series(range(4))
0    0
1    1
2    2
3    3

s.iloc[[1,3]]
1    1
3    3

s.iloc[[1,3,5]]
IndexError

But I'd like to get back a DataFrame (or Series) having an index identical to the list I queried with (ie, parallel to the query list), with (the rows corresponding to) any out-of-bounds indices filled in with NaN : 但是我想找回一个DataFrame(或Series),该索引的索引与我查询的列表相同(即,与查询列表平行),并且(对应的行)已填写任何越界索引使用NaN

s.something[[1,3,5]]
1    1
3    3
5    NaN

I don't think join tricks work because those want to operate on the DataFrame index (or columns). 我不认为join技巧的工作,因为那些想要在数据帧索引(或列)进行操作。 As far as I can tell there's not even an " iget " integer-based get method if I wanted to manually loop over the indices myself. 据我可以告诉有也不是什么“ iget ”基于整数的get ,如果我想手动环比指数自己的方法。 That leaves something like: 留下类似的东西:

indices = [1,3,5]
pd.Series([s.iloc[i] if 0 <= i < len(s) else np.nan for i in indices], index=indices)

Is that the best Pandas 0.18 can do? 那是最好的Pandas 0.18可以做到的吗?

You can use reindex to achieve this: 您可以使用reindex实现此目的:

In [119]:
s.reindex([1,3,5])

Out[119]:
1     1
3     3
5   NaN
dtype: float64

This will use the passed index and return existing values or NaN 这将使用传递的索引并返回现有值或NaN

感谢@EdChum的启发,一般的解决方案是:

s.reset_index(drop=True).reindex([1,3,5])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM