如何检查 pandas DataFrame 中的特定值(在单元格中)是否为 NaN?

2024-02-26

假设我有以下内容pandas DataFrame:

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

看起来像:

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

第一个选项

我知道一种方法来检查特定值是否是NaN:

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

但这会检查整个数据帧只是为了获取一个值,所以我认为这是浪费的。

第二个选项(不起作用)

我想到了下面的选项,使用iloc,也可以工作,但它不会:

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

但是,如果我检查该值,我会得到:

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

So, 为什么第二个选项不起作用?是否可以检查NaN价值观使用iloc?


编者注: 这个问题以前用过pd.np代替np and .ix此外.iloc,但由于这些内容已不复存在,因此已将其删除以使其简短明了。


Try pd.isna() https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.isna.html:

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

又名 pd.isnull https://stackoverflow.com/questions/52086574/pandas-isna-and-isnull-what-is-the-difference

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何检查 pandas DataFrame 中的特定值(在单元格中)是否为 NaN? 的相关文章

随机推荐