【问题标题】:Checking if particular value (in cell) is NaN in pandas DataFrame not working using ix or iloc使用 ix 或 iloc 检查 pandas DataFrame 中的特定值(在单元格中)是否为 NaN
【发布时间】:2018-05-06 12:13:21
【问题描述】:

可以说我有以下pandas 987654323 @:

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

第一种选择 H2>

我知道,以检查是否一个特定的值是@ 987654326单程@,这是如下:

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

第二个选项(不工作) H2>

我想下面的选项,使用ix,将工作为好,但它不是: P>

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

我也尝试iloc用相同的结果:

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

然而,如果我检查用于使用那些值ixiloc我得到:

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

因此,为什么是第二个选项不工作? STRONG>是否可以检查NaN使用值ixiloc? P>

【问题讨论】:

  • 解释:尝试:pd.np.nan == pd.np.nan)跨度>
  • 这给了False!这是为什么呢?
  • 这里说的“非数字”的性质。正因为如此,我们有pd.isnull()pd.notnull()IS (NOT) NULL在SQL等 SPAN>
  • @艾汉,你怎么想? - 我们应该关闭它作为一种欺骗 SPAN>

标签: python pandas dataframe nan


【解决方案1】:

试试这个:

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

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

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

【讨论】:

  • 太棒了。这回答了问题的第二部分。我想另一种方式是pd.isnull(df).iloc[1][0]
  • @CedricZoppolo,我喜欢你的原始版本(在问题中)-df.isnull().ix[1,0] 更好
  • @Cedric 也 np.isnan(df.iloc[1,0]) 检查数字是否为 nan。
  • 我会添加 df.iloc[1,0] is pd.np.nan 解析为 False 因为类型不一样。 type(df.iloc[1,0]) 解析为<type 'numpy.float64'>type(pd.np.nan) 解析为<type 'float'>,正如@MaxU 在问题评论中所建议的那样,以检查为什么会发生这种情况。
  • @CedricZoppolo 和 MaxU 难道pd.isnull(df).iloc[1][0] 的效率低得多吗?
【解决方案2】:

上面的答案很好。为了更好地理解,这里是相同的示例。

>>> import pandas as pd
>>>
>>> import numpy as np
>>>
>>> pd.Series([np.nan, 34, 56])
0     NaN
1    34.0
2    56.0
dtype: float64
>>>
>>> s = pd.Series([np.nan, 34, 56])
>>> pd.isnull(s[0])
True
>>>

我也尝试了几次,以下试验没有奏效。感谢@MaxU

>>> s[0]
nan
>>>
>>> s[0] == np.nan
False
>>>
>>> s[0] is np.nan
False
>>>
>>> s[0] == 'nan'
False
>>>
>>> s[0] == pd.np.nan
False
>>>

【讨论】:

    【解决方案3】:

    pd.isna(cell_value) 可用于检查给定单元格值是否为 nan。或者,pd.notna(cell_value) 检查相反的情况。

    来自 pandas 的源代码:

    def isna(obj):
        """
        Detect missing values for an array-like object.
    
        This function takes a scalar or array-like object and indicates
        whether values are missing (``NaN`` in numeric arrays, ``None`` or ``NaN``
        in object arrays, ``NaT`` in datetimelike).
    
        Parameters
        ----------
        obj : scalar or array-like
            Object to check for null or missing values.
    
        Returns
        -------
        bool or array-like of bool
            For scalar input, returns a scalar boolean.
            For array input, returns an array of boolean indicating whether each
            corresponding element is missing.
    
        See Also
        --------
        notna : Boolean inverse of pandas.isna.
        Series.isna : Detect missing values in a Series.
        DataFrame.isna : Detect missing values in a DataFrame.
        Index.isna : Detect missing values in an Index.
    
        Examples
        --------
        Scalar arguments (including strings) result in a scalar boolean.
    
        >>> pd.isna('dog')
        False
    
        >>> pd.isna(np.nan)
        True
    

    【讨论】:

    • pd.isnullpd.isna 的功能完全相同。实际上pd.isnullpd.isna 的别名,如this post 所述。
    • 同意,对于初学者来说,isna 在使用 pandas 时会更具可读性。
    猜你喜欢
    • 2015-10-12
    • 2015-03-01
    • 2020-07-28
    • 2017-08-12
    • 2015-06-14
    相关资源
    最近更新 更多