How to check if any value is NaN in a Pandas DataFrame

Hello Guys, How are you all? Hope You all Are Fine. Today We Are Going To learn about How to check if any value is NaN in a Pandas DataFrame in Python. So Here I am Explain to you all the possible Methods here.

How to check if any value is NaN in a Pandas DataFrame?

1. How to check if any value is NaN in a Pandas DataFrame?

To find out which rows have NaNs in a specific column:
nan_rows = df[df['name column'].isnull()]

2. check if any value is NaN in a Pandas DataFrame

To find out which rows have NaNs in a specific column:
nan_rows = df[df['name column'].isnull()]

Method 1

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()
import numpy as np
import pandas as pd
import perfplot

def setup(n):
df = pd.DataFrame(np.random.randn(n))
df[df > 0.9] = np.nan
return df

def isnull_any(df):
return df.isnull().any()

def isnull_values_sum(df):
return df.isnull().values.sum() > 0

def isnull_sum(df):
return df.isnull().sum() > 0

def isnull_values_any(df):
return df.isnull().values.any()

perfplot.save(
"out.png",
setup=setup,
kernels=[isnull_any, isnull_values_sum, isnull_sum, isnull_values_any],
n_range=[2 ** k for k in range(25)],
)

df.isnull().sum().sum() is a bit slower, but of course, has additional information — the number of NaNs.

Method 2

To find out which rows have NaNs in a specific column:

nan_rows = df[df['name column'].isnull()]

Conclusion

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.