Checking if particular value (in cell) is NaN in pandas DataFrame not working using ix or iloc

后端 未结 3 1919
长情又很酷
长情又很酷 2020-12-09 01:13

Lets say I have following pandas DataFrame:

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


        
相关标签:
3条回答
  • 2020-12-09 01:37

    Try this:

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

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

    In [7]: pd.isna(df.iloc[1,0])
    Out[7]: True
    
    0 讨论(0)
  • 2020-12-09 01:40

    The above answer is excellent. Here is the same with an example for better understanding.

    >>> 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
    >>>
    

    I also tried couple of times, the following trials did not work. Thanks to @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
    >>>
    
    0 讨论(0)
  • 2020-12-09 01:53

    pd.isna(cell_value) can be used to check if a given cell value is nan. Alternatively, pd.notna(cell_value) to check the opposite.

    From source code of 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
    
    0 讨论(0)
提交回复
热议问题