pandas 熊猫:返回 NaN 行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36745912/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 01:05:06  来源:igfitidea点击:

Pandas: return NaN rows

pythonpandasdataframeipython

提问by Michael Perdue

I am trying to return a df that contains all of the NaNvalues for column == years_expso that I can identify the corresponding id.thomas(basically I'm debugging some data that I parsed by hand). I also need to return a df with all minvalues. This is what I have tried so far:

我试图返回一个包含所有NaN值的 dfcolumn == years_exp以便我可以识别相应的值id.thomas(基本上我正在调试一些我手动解析的数据)。我还需要返回一个包含所有min值的 df 。这是我迄今为止尝试过的:

rr.head(5)

    years   id.thomas   years_exp
55  2005          2     17
56  2006          2     18
57  2007          2     19
58  2008          2     20
59  2009          2     21

c = rr
c = c[c.years_exp == 'NaN']

Error:

错误:

TypeError: invalid type comparison

类型错误:无效的类型比较

I'm using syntax that I copied from a youtube video on Pandas. Does anyone have an idea about the error?

我正在使用从 Pandas 上的 YouTube 视频中复制的语法。有没有人知道这个错误?

回答by jezrael

You need isnullfor checking NaNvalues:

您需要isnull检查NaN值:

print (rr[rr.years_exp.isnull()])

Docs:

文档

Warning

One has to be mindful that in python (and numpy), the nan's don't compare equal, but None's do. Note that Pandas/numpy uses the fact that np.nan != np.nan, and treats None like np.nan.

警告

必须注意,在 python(和 numpy)中,nan 不相等,但 None 不相等。请注意,Pandas/numpy 使用 np.nan != np.nan 的事实,并将 None 视为 np.nan。

In [11]: None == None
Out[11]: True

In [12]: np.nan == np.nan
Out[12]: False

So as compared to above, a scalar equality comparison versus a None/np.nan doesn't provide useful information.

因此,与上面相比,标量相等比较与 None/np.nan 没有提供有用的信息。

In [13]: df2['one'] == np.nan
Out[13]: 
a    False
b    False
c    False
d    False
e    False
f    False
g    False
h    False
Name: one, dtype: bool

回答by Antonin Bouscarel

You can try with

你可以试试

c = c.loc[c.years_exp == 'NaN']

or

或者

c = c.loc[c.years_exp == None]

or

或者

c = c.loc[c.years_exp.isnull()]