熊猫:返回NaN行

Mic*_*due 5 python ipython dataframe pandas

我试图返回一个包含所有NaN值的df,column == 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']
Run Code Online (Sandbox Code Playgroud)

错误:

TypeError:无效的类型比较

我使用的是从Pandas上的youtube视频复制的语法。有人对这个错误有想法吗?

jez*_*ael 6

您需要isnull检查NaN值:

print (rr[rr.years_exp.isnull()])
Run Code Online (Sandbox Code Playgroud)

文件

警告

必须记住,在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
Run Code Online (Sandbox Code Playgroud)

因此,与上述相比,标量相等比较与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
Run Code Online (Sandbox Code Playgroud)


小智 5

你可以尝试使用

c = c.loc[c.years_exp == 'NaN']
Run Code Online (Sandbox Code Playgroud)

或者

c = c.loc[c.years_exp == None]
Run Code Online (Sandbox Code Playgroud)

或者

c = c.loc[c.years_exp.isnull()]
Run Code Online (Sandbox Code Playgroud)