How to Check if NaN Exists in a Pandas DataFrame
NaN
Stands for Not a Number- Not a Number
, which indicates missing values in Pandas. To detect NaN values in Python Pandas, we can use the isnull()
and isna()
methods on the DataFrame object.
pandas.DataFrame.isnull() method
We can use pandas.DataFrame.isnull() to check for NaN values in a DataFrame. DataFrame
The method returns a boolean value of the DataFrame element if the corresponding element in the DataFrame to be checked has a NaN value True
, else it is False
.
import pandas as pd
import numpy as np
df = pd.DataFrame(
{
"Student": ["Hisila", "Shristi", "Zeppy", "Alina", "Jerry"],
"Height": [1.63, 1.5, np.nan, np.nan, 1.4],
"Weight": [np.nan, 56, 73, np.nan, 44],
}
)
df_check = df.isnull()
print(df_check)
Output:
Student Height Weight
0 False False True
1 False False False
2 False True False
3 False True True
4 False False False
Here, False
the values in the output represent df
the elements in the DataFrame NaN
and True
the values represent the elements df
in the DataFrame NaN
.
If we want to know whether there are NaN values in the DataFrame, we can use isnull().values.any()
the method, which returns True if there are any NaN values in the DataFrame and False if there is not even a single NaN element in the DataFrame.
import pandas as pd
import numpy as np
df = pd.DataFrame(
{
"Student": ["Hisila", "Shristi", "Zeppy", "Alina", "Jerry"],
"Height": [1.63, 1.5, np.nan, np.nan, 1.4],
"Weight": [np.nan, 56, 73, np.nan, 44],
}
)
check_for_nan = df.isnull().values.any()
print(check_for_nan)
Output:
True
df.isnull().values
numpy.any()
Returns a NumPy representation of the DataFrame. Returns True if any element evaluates to True .
Therefore, if any exists in the DataFrame NaN
, then df.isnull().values.any()
is True
.
df.isnull().any().any()
Check if there are any NaNs
df.any()
Returns whether any element is True. df
It returns one when is a DataFrame pd.Series
and a boolean when df
is .pd.Series
import pandas as pd
import numpy as np
df = pd.DataFrame(
{
"Student": ["Hisila", "Shristi", "Zeppy", "Alina", "Jerry"],
"Height": [1.63, 1.5, np.nan, np.nan, 1.4],
"Weight": [np.nan, 56, 73, np.nan, 44],
}
)
check_for_nan = df.isnull().any().any()
print(check_for_nan)
Output:
True
The two concatenated methods after NaN
in the above example will return if any element in the DataFrame is .isnull()
any()
True
isnull().sum().sum()
Checking for the presence of NaN
If we want to count the total number of NaN values in a particular DataFrame, then df.isnull().sum().sum()
the method is the right solution. This method returns the total number of values in the entire DataFrame NaN
.
import pandas as pd
import numpy as np
df = pd.DataFrame(
{
"Student": ["Hisila", "Shristi", "Zeppy", "Alina", "Jerry"],
"Height": [1.63, 1.5, np.nan, np.nan, 1.4],
"Weight": [np.nan, 56, 73, np.nan, 44],
}
)
total_nan_values = df.isnull().sum().sum()
print(total_nan_values)
Output:
4
If the result is greater than 0, it means it exists in the DataFrame NaN
.
pandas.DataFrame.isna()
method
The pandas.DataFrame.isna() method is similar to
pandas.DataFrame.isnull(). There is no difference between the working of both the methods. They are just named differently.
import pandas as pd
import numpy as np
df = pd.DataFrame(
{
"Student": ["Hisila", "Shristi", "Zeppy", "Alina", "Jerry"],
"Height": [1.63, 1.5, np.nan, np.nan, 1.4],
"Weight": [np.nan, 56, 73, np.nan, 44],
}
)
df_check = df.isna()
check_for_any_nan = df.isna().values.any()
# Or
check_for_any_nan = df.isna().any().any()
total_nan_values = df.isna().sum().sum()
print(df_check)
print("NaN Presence:" + str(check_for_any_nan))
print("Total Number of NaN values:" + str(total_nan_values))
Output:
Student Height Weight
0 False False True
1 False False False
2 False True False
3 False True True
4 False False False
NaN Presence:True
Total Number of NaN values:4
Here, the method df.isna()
returns a DataFrame whose elements contain Boolean values indicating the presence of NaN values in df. Similarly, df.isna().values.any()
, df.isna().any().any()
and df.isna().sum().sum()
return the number of NaN values present in the entire df and the number of NaN elements in df.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Pandas DataFrame DataFrame.query() function
Publish Date:2025/04/30 Views:107 Category:Python
-
The pandas.DataFrame.query() method filters the rows of the caller DataFrame using the given query expression. pandas.DataFrame.query() grammar DataFrame . query(expr, inplace = False , ** kwargs) parameter expr Filter rows based on query e
Pandas DataFrame DataFrame.min() function
Publish Date:2025/04/30 Views:162 Category:Python
-
Python Pandas DataFrame.min() function gets the minimum value of the DataFrame object along the specified axis. pandas.DataFrame.min() grammar DataFrame . mean(axis = None , skipna = None , level = None , numeric_only = None , ** kwargs) pa
Pandas DataFrame DataFrame.mean() function
Publish Date:2025/04/30 Views:85 Category:Python
-
Python Pandas DataFrame.mean() function calculates the mean of the values of the DataFrame object over the specified axis. pandas.DataFrame.mean() grammar DataFrame . mean(axis = None , skipna = None , level = None , numeric_only = No
Pandas DataFrame DataFrame.isin() function
Publish Date:2025/04/30 Views:133 Category:Python
-
The pandas.DataFrame.isin(values) function checks whether each element in the caller DataFrame contains values the value specified in the input . pandas.DataFrame.isin(values) grammar DataFrame . isin(values) parameter values iterable - lis
Pandas DataFrame DataFrame.groupby() function
Publish Date:2025/04/30 Views:161 Category:Python
-
pandas.DataFrame.groupby() takes a DataFrame as input and divides the DataFrame into groups based on a given criterion. We can use groupby() the method to easily process large datasets. pandas.DataFrame.groupby() grammar DataFrame . groupby
Pandas DataFrame DataFrame.fillna() function
Publish Date:2025/04/30 Views:60 Category:Python
-
The pandas.DataFrame.fillna() function replaces the values DataFrame in NaN with a certain value. pandas.DataFrame.fillna() grammar DataFrame . fillna( value = None , method = None , axis = None , inplace = False , limit = None , down
Pandas DataFrame DataFrame.dropna() function
Publish Date:2025/04/30 Views:181 Category:Python
-
The pandas.DataFrame.dropna() function removes null values (missing values) from a DataFrame by dropping rows or columns that contain null values DataFrame . NaN ( Not a Number ) and NaT ( Not a Time ) represent null values. DataFrame
Pandas DataFrame DataFrame.assign() function
Publish Date:2025/04/30 Views:55 Category:Python
-
Python Pandas DataFrame.assign() function assigns new columns to DataFrame . pandas.DataFrame.assign() grammar DataFrame . assign( ** kwargs) parameter **kwargs Keyword arguments, DataFrame the column names to be assigned to are passed as k
Pandas DataFrame DataFrame.transform() function
Publish Date:2025/04/30 Views:120 Category:Python
-
Python Pandas DataFrame.transform() DataFrame applies a function on and transforms DataFrame . The function to be applied is passed as an argument to the function. The axis length of transform() the transformed should be the same as the ori