pandas 选择 nan 索引的熊猫数据框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25536133/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
pandas dataframe selecting the nan indexes
提问by lessthanl0l
I have a dataframe dfwith the following:
我有一个df包含以下内容的数据框:
In [10]: df.index.unique()
Out[10]: array([u'DC', nan, u'BS', u'AB', u'OA'], dtype=object)
I can easily select out df.ix["DC"], df.ix["BS"], etc. But I'm having trouble selecting the nanindexes.
我可以轻松地选择出 df.ix["DC"]、df.ix["BS"] 等。但是我在选择nan索引时遇到了麻烦。
df.ix[nan], df.ix["nan"], df.ix[np.nan] all won't work.
How do I select the rows with nanas the index?
如何选择nan作为索引的行?
回答by unutbu
One way would be to use df.index.isnull()to identify the location of the NaNs:
一种方法是使用df.index.isnull()来识别 NaN 的位置:
In [218]: df = pd.DataFrame({'Date': [0, 1, 2, 0, 1, 2], 'Name': ['A', 'B', 'C', 'A', 'B', 'C'], 'val': [0, 1, 2, 3, 4, 5]}, index=['DC', np.nan, 'BS', 'AB', 'OA', np.nan]); df
Out[218]:
Date Name val
DC 0 A 0
NaN 1 B 1
BS 2 C 2
AB 0 A 3
OA 1 B 4
NaN 2 C 5
In [219]: df.index.isnull()
Out[219]: array([False, True, False, False, False, True], dtype=bool)
Then you could select those rows using df.loc:
然后您可以使用df.loc以下方法选择这些行:
In [220]: df.loc[df.index.isnull()]
Out[220]:
Date Name val
NaN 1 B 1
NaN 2 C 5
Note: My original answer used pd.isnull(df.index)instead of Zero's suggestion, df.index.isnull(). It is better to use df.index.isnull()because for types of Indexes which can not hold NaNs, such as Int64Indexand RangeIndex, the isnullmethod returns an array of all False values immediatelyinstead of mindlessly checking each item in the index for NaN values.
注意:我的原始答案用于pd.isnull(df.index)代替Zero 的建议, df.index.isnull()。最好使用它,df.index.isnull()因为对于不能包含 NaN 的索引类型,例如Int64Indexand RangeIndex,该isnull方法立即返回一个包含所有 False 值的数组,而不是盲目地检查索引中的每个项目是否有 NaN 值。

