The Wayback Machine - https://web.archive.org/web/20240803013737/https://www.geeksforgeeks.org/check-for-nan-in-pandas-dataframe/
Open In App

Check for NaN in Pandas DataFrame

Last Updated : 30 Jan, 2023
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report

NaN stands for Not A Number and is one of the common ways to represent the missing value in the data. It is a special floating-point value and cannot be converted to any other type than float. 

NaN value is one of the major problems in Data Analysis. It is very essential to deal with NaN in order to get the desired results.

 

Check for NaN Value in Pandas DataFrame

The ways to check for NaN in Pandas DataFrame are as follows: 

  • Check for NaN with isnull().values.any() method
  • Count the NaN Using isnull().sum() Method
  • Check for NaN Using isnull().sum().any() Method
  • Count the NaN Using isnull().sum().sum() Method

Method 1: Using isnull().values.any() method

Example: 

Python3




# importing libraries
import pandas as pd
import numpy as np
 
 
num = {'Integers': [10, 15, 30, 40, 55, np.nan,
                    75, np.nan, 90, 150, np.nan]}
 
# Create the dataframe
df = pd.DataFrame(num, columns=['Integers'])
 
# Applying the method
check_nan = df['Integers'].isnull().values.any()
 
# printing the result
print(check_nan)


Output: 

True

It is also possible to get the exact positions where NaN values are present. We can do so by removing .values.any() from isnull().values.any() . 

Python3




df['Integers'].isnull()


Output: 

0     False
1     False
2     False
3     False
4     False
5      True
6     False
7      True
8     False
9     False
10     True
Name: Integers, dtype: bool

Method 2: Using isnull().sum() Method

Example: 

Python3




# importing libraries
import pandas as pd
import numpy as np
 
 
num = {'Integers': [10, 15, 30, 40, 55, np.nan,
                    75, np.nan, 90, 150, np.nan]}
 
# Create the dataframe
df = pd.DataFrame(num, columns=['Integers'])
 
# applying the method
count_nan = df['Integers'].isnull().sum()
 
# printing the number of values present
# in the column
print('Number of NaN values present: ' + str(count_nan))


Output:

Number of NaN values present: 3

Method 3: Using isnull().sum().any() Method

Example: 

Python3




# importing libraries
import pandas as pd
import numpy as np
 
nums = {'Integers_1': [10, 15, 30, 40, 55, np.nan, 75,
                       np.nan, 90, 150, np.nan],
        'Integers_2': [np.nan, 21, 22, 23, np.nan, 24, 25,
                       np.nan, 26, np.nan, np.nan]}
 
# Create the dataframe
df = pd.DataFrame(nums, columns=['Integers_1', 'Integers_2'])
 
# applying the method
nan_in_df = df.isnull().sum().any()
 
# Print the dataframe
print(nan_in_df)


Output: 

True

To get the exact positions where NaN values are present, we can do so by removing .sum().any() from isnull().sum().any() . 

Method 4: Using isnull().sum().sum() Method

Example: 

Python3




# importing libraries
import pandas as pd
import numpy as np
 
nums = {'Integers_1': [10, 15, 30, 40, 55, np.nan, 75,
                       np.nan, 90, 150, np.nan],
        'Integers_2': [np.nan, 21, 22, 23, np.nan, 24, 25,
                       np.nan, 26, np.nan, np.nan]}
 
# Create the dataframe
df = pd.DataFrame(nums, columns=['Integers_1', 'Integers_2'])
 
# applying the method
nan_in_df = df.isnull().sum().sum()
 
# printing the number of values present in
# the whole dataframe
print('Number of NaN values present: ' + str(nan_in_df))


Output:

Number of NaN values present: 8


Similar Reads

Ways to Create NaN Values in Pandas DataFrame
Let's discuss ways of creating NaN values in the Pandas Dataframe. There are various ways to create NaN values in Pandas dataFrame. Those are: Using NumPy Importing csv file having blank values Applying to_numeric function Method 1: Using NumPy C/C++ Code import pandas as pd import numpy as np num = {'number': [1,2,np.nan,6,7,np.nan,np.nan]} df = p
1 min read
Drop rows from Pandas dataframe with missing values or NaN in columns
Pandas provides various data structures and operations for manipulating numerical data and time series. However, there can be cases where some data might be missing. In Pandas missing data is represented by two value: None: None is a Python singleton object that is often used for missing data in Python code. NaN: NaN (an acronym for Not a Number),
4 min read
Count NaN or missing values in Pandas DataFrame
In this article, we will see how to Count NaN or missing values in Pandas DataFrame using isnull() and sum() method of the DataFrame. Dataframe.isnull() method Pandas isnull() function detect missing values in the given object. It return a boolean same-sized object indicating if the values are NA. Missing values gets mapped to True and non-missing
5 min read
Replace all the NaN values with Zero's in a column of a Pandas dataframe
Replacing the NaN or the null values in a dataframe can be easily performed using a single line DataFrame.fillna() and DataFrame.replace() method. We will discuss these methods along with an example demonstrating how to use it. DataFrame.fillna(): This method is used to fill null or null values with a specific value. Syntax: DataFrame.fillna(self,
3 min read
Count the NaN values in one or more columns in Pandas DataFrame
Let us see how to count the total number of NaN values in one or more columns in a Pandas DataFrame. In order to count the NaN values in the DataFrame, we are required to assign a dictionary to the DataFrame and that dictionary should contain numpy.nan values which is a NaN(null) value. Consider the following DataFrame. # importing the modules impo
2 min read
Highlight the nan values in Pandas Dataframe
In this article, we will discuss how to highlight the NaN (Not a number) values in Pandas Dataframe. NaN values used to represent NULL values and sometimes it is the result of the mathematical overflow.Lets first make a dataframe: C/C++ Code # Import Required Libraries import pandas as pd import numpy as np # Create a dictionary for the dataframe d
2 min read
How to Drop Columns with NaN Values in Pandas DataFrame?
Nan(Not a number) is a floating-point value which can't be converted into other data type expect to float. In data analysis, Nan is the unnecessary value which must be removed in order to analyze the data set properly. In this article, we will discuss how to remove/drop columns having Nan values in the pandas Dataframe. We have a function known as
3 min read
How to Count the NaN Occurrences in a Column in Pandas Dataframe?
The data frame is divided into cells, which can store a value belonging to some data structure as well as it may contain missing or NA values. The pandas package contains various in-built functions, to check if the value in the cell of a data frame is either NA or not, and also to perform aggregations over these NA values. Method #1: Using In-built
2 min read
Replace NaN Values with Zeros in Pandas DataFrame
NaN stands for Not A Number and is one of the common ways to represent the missing value in the data. It is a special floating-point value and cannot be converted to any other type than float. NaN value is one of the major problems in Data Analysis. It is very essential to deal with NaN in order to get the desired results.  Methods to Replace NaN V
3 min read
How to Drop Rows with NaN Values in Pandas DataFrame?
NaN stands for Not A Number and is one of the common ways to represent the missing value in the data. It is a special floating-point value and cannot be converted to any other type than float. NaN value is one of the major problems in Data Analysis. It is very essential to deal with NaN in order to get the desired results. In this article, we will
4 min read
Practice Tags :
three90RightbarBannerImg