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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# importing the modules
import numpy as np
import pandas as pd
   
# creating the DataFrame
dictionary = {'Names': ['Simon', 'Josh', 'Amen'
                        'Habby', 'Jonathan', 'Nick', 'Jake'],
              'Capitals': ['VIENNA', np.nan, 'BRASILIA'
                           np.nan, 'PARIS', 'DELHI', 'BERLIN'],
              'Countries': ['AUSTRIA', 'BELGIUM', 'BRAZIL'
                            np.nan, np.nan, 'INDIA', np.nan]}
table = pd.DataFrame(dictionary, columns = ['Names'
                                           'Capitals'
                                           'Countries'])
  
# displaying the DataFrame
display(table)

chevron_right


Output :

Example 1 : Counting the NaN values in a single column.

filter_none

edit
close

play_arrow

link
brightness_4
code

print("Number of null values in column 1 : " + 
       str(table.iloc[:, 1].isnull().sum()))
print("Number of null values in column 2 : " + 
       str(table.iloc[:, 2].isnull().sum()))

chevron_right


Output :



Number of null values in column 1 : 2
Number of null values in column 2 : 3

Example 2 : Counting the NaN values in a single row.

filter_none

edit
close

play_arrow

link
brightness_4
code

print("Number of null values in row 0 : " + 
       str(table.iloc[0, ].isnull().sum()))
print("Number of null values in row 1 : " + 
       str(table.iloc[1, ].isnull().sum()))
print("Number of null values in row 3 : " + 
       str(table.iloc[3, ].isnull().sum()))

chevron_right


Output :

Number of null values in row 0 : 0
Number of null values in row 1 : 1
Number of null values in row 3 : 2

Example 3 : Counting the total NaN values in the DataFrame.

filter_none

edit
close

play_arrow

link
brightness_4
code

print("Total Number of null values in the DataFrame : " + 
       str(table.isnull().sum().sum()))

chevron_right


Output :

Total Number of null values in the DataFrame : 5

Example 4 : Counting the NaN values in all the columns.

filter_none

edit
close

play_arrow

link
brightness_4
code

display(table.isnull().sum())

chevron_right


Output :




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.