使用ix或iloc检查pandas DataFrame中的特定值(单元格中)是否为NaN不能使用ix或iloc [英] Checking if particular value (in cell) is NaN in pandas DataFrame not working using ix or iloc

查看:354
本文介绍了使用ix或iloc检查pandas DataFrame中的特定值(单元格中)是否为NaN不能使用ix或iloc的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有以下pandas DataFrame:

import pandas as pd
df = pd.DataFrame({"A":[1,pd.np.nan,2], "B":[5,6,0]})

看起来像这样:

>>> df
     A  B
0  1.0  5
1  NaN  6
2  2.0  0

第一个选项

我知道一种检查特定值是否为NaN的方法,如下所示:

First option

I know one way to check if a particular value is NaN, which is as follows:

>>> df.isnull().ix[1,0]
True

第二个选项(不起作用)

我认为使用ix的以下选项也可以使用,但不是:

Second option (not working)

I thought below option, using ix, would work as well, but it's not:

>>> df.ix[1,0]==pd.np.nan
False

我也尝试了iloc的结果:

>>> df.iloc[1,0]==pd.np.nan
False

但是,如果我使用ixiloc检查这些值,则会得到:

However if I check for those values using ix or iloc I get:

>>> df.ix[1,0]
nan
>>> df.iloc[1,0]
nan

那么,为什么第二个选项不起作用?是否可以使用ixiloc检查NaN值?

So, why is the second option not working? Is it possible to check for NaN values using ix or iloc?

推荐答案

尝试一下:

In [107]: pd.isnull(df.iloc[1,0])
Out[107]: True


更新:在较新的Pandas版本中,使用 pd.isna():


UPDATE: in a newer Pandas versions use pd.isna():

In [7]: pd.isna(df.iloc[1,0])
Out[7]: True

这篇关于使用ix或iloc检查pandas DataFrame中的特定值(单元格中)是否为NaN不能使用ix或iloc的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆