Check if single cell value is NaN in Pandas
Asked Answered
C

5

58

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?

Clothbound answered 3/1, 2015 at 12:32 Comment(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.Rossiter
eg if numpy.isnan(vendor_details['EMAIL']): here vendor_details is a pandas Series.Clothbound
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.Saphead
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).Entreaty
F
89

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
Froebel answered 3/1, 2015 at 12:57 Comment(3)
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?Resolve
There is no difference between isnull and isnan; isnull is an alias for isnan.Hominid
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).Entreaty
C
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

Cellulitis answered 31/7, 2018 at 23:15 Comment(0)
A
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"
Azaleeazan answered 12/6, 2018 at 7:16 Comment(0)
C
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!

Coleville answered 28/7, 2020 at 4:47 Comment(2)
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.Obeng
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.Sentimentalism
R
0

You can check all values and then select the value you are interested in. For example:

df = pd.DataFrame({'A': [1, 2], 'B': [np.nan, 4]})

   A    B
0  1  NaN
1  2  4.0

df.isna().loc[0, 'B']

Output:

True
Royster answered 14/9, 2023 at 19:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.