57

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?

smci
  • 32,567
  • 20
  • 113
  • 146
vidit
  • 643
  • 1
  • 5
  • 13
  • 4
    Can you add a minimal example of input and output for your question? numpy.isnan works for a single value, but I guess this is not your question. – Benjamin Cbr Jan 03 '15 at 12:40
  • 2
    eg if numpy.isnan(vendor_details['EMAIL']): here vendor_details is a pandas Series. – vidit Jan 03 '15 at 12:42
  • 1
    I'm voting to close this: All three methods described in the OP should work, and the accepted solution is just to use two of those. Again, **the accepted answer is to do exactly what OP said wasn't working**. – AMC Feb 13 '20 at 01:36
  • NOTE: With newer versions of pandas nulls can be pd.NA or pd.NaT instead of np.NaN. np.isnan(pd.NA) will return pd.NA, and np.isnan(pd.NaT) will cause an error. pd.isnull(pd.NA) and pd.isnull(pd.NaT) will return True in both cases (and will also return True for np.NaN and None). – Brett Romero Apr 25 '22 at 10:00

4 Answers4

87

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
duan
  • 8,515
  • 3
  • 48
  • 70
Shahriar
  • 13,460
  • 8
  • 78
  • 95
  • 2
    Very useful, what is the difference between isnull and isnan methods? Is there a way to test truthiness of NaN like just `if df[1] then` or similar? – Davos Mar 20 '18 at 14:42
  • There is no difference between isnull and isnan; isnull is an alias for isnan. – deepyaman Sep 29 '18 at 11:13
  • NOTE: With newer versions of pandas nulls can be pd.NA or pd.NaT instead of np.NaN. np.isnan(pd.NA) will return pd.NA, and np.isnan(pd.NaT) will cause an error. pd.isnull(pd.NA) and pd.isnull(pd.NaT) will return True in both cases (and will also return True for np.NaN and None). – Brett Romero Apr 25 '22 at 09:59
11

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

sparrow
  • 10,794
  • 12
  • 54
  • 74
5

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"
yashu vishnalia
  • 611
  • 7
  • 7
1

Just encountered this problem myself and found a solution, imperfect, but works. As noted above, none of these 3 answers are addressing OP's question. Here's an example of my problem which I feel is the same.

# fill null values of one column with that of another
f = lambda row: row['A'] if (row['B'].isnull()) else row['B']
df['B'] = df.apply(f, axis=1)

>>> AttributeError: 'str' object has no attribute 'isnull'

Because the value within a cell of a dataframe is just a primative datatype, you can't use any of pandas built-in methods. So this is what I did.

f = lambda row: row['A'] if (str(row['B'])=='nan') else row['B']

This actually the only thing I could get to work!

ambose
  • 87
  • 3
  • This works for me as well most of the time. Note that this solution won't work though if the cell value can't be read using str(). Namely, if using Python 2.7 and the cell has a unicode string. Python 2.7 uses ascii encoding for str() and will throw an error if you use it to check a unicode string. – Bryany Jul 28 '21 at 14:55
  • There is nothing above that says that none of the answers address OP's question, maybe something was deleted though. In the example, you try to call a str's method `isnull` which doesn't exist. This is *not* what any of the answers suggested. Change `row['B'].isnull()` to `pd.isnull(row['B'])` - as suggested by two of the three answers above - and it will work. – Evgenii Oct 04 '22 at 15:02