close

How to check if a particular cell in pandas DataFrame isnull?

Hello Guys, How are you all? Hope You all Are Fine. Today We Are Going To learn about How to check if a particular cell in pandas DataFrame isnull in Python. So Here I am Explain to you all the possible Methods here.

Without wasting your time, Let’s start This Article.

Table of Contents

How to check if a particular cell in pandas DataFrame isnull?

  1. How to check if a particular cell in pandas DataFrame isnull?

    jezrael response is spot on. If you are only concern with NaN value, I was exploring to see if there's a faster option, since in my experience, summing flat arrays is (strangely) faster than counting.

  2. check if a particular cell in pandas DataFrame isnull

    jezrael response is spot on. If you are only concern with NaN value, I was exploring to see if there's a faster option, since in my experience, summing flat arrays is (strangely) faster than counting.

Method 1

Use pd.isnull, for select use loc or iloc:

print (df)
   0  A   B  C
0  1  2 NaN  8

print (df.loc[0, 'B'])
nan

a = pd.isnull(df.loc[0, 'B'])
print (a)
True

print (df['B'].iloc[0])
nan

a = pd.isnull(df['B'].iloc[0])
print (a)
True

Method 2

jezrael response is spot on. If you are only concern with NaN value, I was exploring to see if there’s a faster option, since in my experience, summing flat arrays is (strangely) faster than counting. This code seems faster:

df.isnull().values.any()
For example:

In [2]: df = pd.DataFrame(np.random.randn(1000,1000))

In [3]: df[df > 0.9] = pd.np.nan

In [4]: %timeit df.isnull().any().any()
100 loops, best of 3: 14.7 ms per loop

In [5]: %timeit df.isnull().values.sum()
100 loops, best of 3: 2.15 ms per loop

In [6]: %timeit df.isnull().sum().sum()
100 loops, best of 3: 18 ms per loop

In [7]: %timeit df.isnull().values.any()
1000 loops, best of 3: 948 µs per loop

Summery

It’s all About this issue. Hope all Methods helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which Method worked for you? Thank You.

Also, Read