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

后端 未结 3 1930
长情又很酷
长情又很酷 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: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
    >>>
    

提交回复
热议问题