Check if single cell value is NaN in Pandas

2020-02-18 09:34发布

I just want to check if a single cell in Pandas series is null or not i.e. to check if a value is NaN.

All other answers are for series and arrays, but not for single value.

I have tried pandas.notnull, pandas.isnull, numpy.isnan. Is there a solution for a single value only?

3条回答
Anthone
2楼-- · 2020-02-18 10:14

You can use "isnull" with "at" to check a specific value in a dataframe.

For example:

import pandas as pd
import numpy as np

df = pd.DataFrame([[np.nan, 2], [1, 3], [4, 6]], columns=['A', 'B'])

Yeilds:

    A   B
0   NaN 2
1   1.0 3
2   4.0 6

To check the values:

pd.isnull(df.at[0,'A'])

-> True

pd.isnull(df.at[0,'B'])

-> False

查看更多
对你真心纯属浪费
3楼-- · 2020-02-18 10:26

Try this:

import pandas as pd
import numpy as np
from pandas import *

>>> L = [4, nan ,6]
>>> df = Series(L)

>>> df
0     4
1   NaN
2     6

>>> if(pd.isnull(df[1])):
        print "Found"

Found

>>> if(np.isnan(df[1])):
        print "Found"

Found
查看更多
Lonely孤独者°
4楼-- · 2020-02-18 10:28

STEP 1.)

df[df.isnull().any(1)]

----> Will give you dataframe with rows and column, if any value there is nan.

STEP 2.)

this will give you location in dataframe where exactly value is nan. then you could do

if(**df.iloc[loc_row,loc_colum]==np.nan**):
    print"your code here"
查看更多
登录 后发表回答