代码之家  ›  专栏  ›  技术社区  ›  Cedric Zoppolo

使用ix或iloc检查pandas数据帧中的特定值(单元格中)是否为NaN

  •  72
  • Cedric Zoppolo  · 技术社区  · 7 年前

    假设我有以下内容 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 ,如下所示:

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

    第二个选项(不工作)

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

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

    我也试过了 iloc 结果相同:

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

    但是,如果我使用 九、 iloc公司 我得到:

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

    为什么第二种选择不起作用? 是否可以检查 值使用 九、 iloc公司 ?

    4 回复  |  直到 7 年前
        1
  •  122
  •   MaxU - stand with Ukraine    5 年前

    试试这个:

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

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

    In [7]: pd.isna(df.iloc[1,0])
    Out[7]: True
    
        2
  •  12
  •   hygull    6 年前

    以上答案很好。为了更好地理解,这里也有一个例子。

    >>> 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
  •  7
  •   Prateek Kumar Dalbehera    5 年前

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

    来自熊猫的源代码:

    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
    
        4
  •  -1
  •   slavny_coder    3 年前

    我制定了一些变通方法:

    x = [np.nan]
    
    In [4]: x[0] == np.nan
    Out[4]: False
    

    但是:

    In [5]: np.nan in x
    Out[5]: True
    

    您可以看到列表包含方法 implementation ,以了解其工作原理。