• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / Get the Count of NaN in pandas Using Python

Get the Count of NaN in pandas Using Python

December 16, 2020 Leave a Comment

To get the count of NaN in a pandas dataframe, the simplest way is to use the pandas isnull() function and pandas sum() function.

df["variable"].isnull().sum()

When working with data as a data science or data analyst, it’s important to be able to find the basic descriptive statistics of a set of data.

One basic descriptive statistic which is important is the number of missing or NaN values in a dataset.

The pandas describe() function can get us a number of great descriptive statistics, but it cannot return the number of missing values of a series.

To get the number of missing values of a series in Python, we use the isnull() and sum() functions.

The following code will get you the count of missing values of a series in Python:

df["variable"].isnull().sum()

Getting the Count of NaN of a Column Using Pandas

Let’s say I have the following pandas dataframe:

   animal_type  gender         type variable level  count    sum   mean        std   min    25%   50%    75%    max
0          cat  female      numeric      age   N/A    5.0   18.0   3.60   1.516575   2.0   3.00   3.0   4.00    6.0
1          cat    male      numeric      age   N/A    2.0    3.0   1.50   0.707107   1.0   1.25   1.5   1.75    2.0
2          dog  female      numeric      age   N/A    2.0    8.0   4.00   0.000000   4.0   4.00   4.0   4.00    4.0
3          dog    male      numeric      age   N/A    4.0   15.0   3.75   1.892969   1.0   3.25   4.5   5.00    5.0
4          cat  female      numeric   weight   N/A    5.0  270.0  54.00  32.093613  10.0  40.00  50.0  80.00   90.0
5          cat    male      numeric   weight   N/A    2.0  110.0  55.00  63.639610  10.0  32.50  55.0  77.50  100.0
6          dog  female      numeric   weight   N/A    2.0  100.0  50.00  42.426407  20.0  35.00  50.0  65.00   80.0
7          dog    male      numeric   weight   N/A    4.0  180.0  45.00  23.804761  20.0  27.50  45.0  62.50   70.0
8          cat  female  categorical    state    FL    2.0    NaN    NaN        NaN   NaN    NaN   NaN    NaN    NaN
9          cat  female  categorical    state    NY    1.0    NaN    NaN        NaN   NaN    NaN   NaN    NaN    NaN
10         cat  female  categorical    state    TX    2.0    NaN    NaN        NaN   NaN    NaN   NaN    NaN    NaN
11         cat    male  categorical    state    CA    1.0    NaN    NaN        NaN   NaN    NaN   NaN    NaN    NaN
12         cat    male  categorical    state    TX    1.0    NaN    NaN        NaN   NaN    NaN   NaN    NaN    NaN
13         dog  female  categorical    state    FL    1.0    NaN    NaN        NaN   NaN    NaN   NaN    NaN    NaN
14         dog  female  categorical    state    TX    1.0    NaN    NaN        NaN   NaN    NaN   NaN    NaN    NaN
15         dog    male  categorical    state    CA    1.0    NaN    NaN        NaN   NaN    NaN   NaN    NaN    NaN
16         dog    male  categorical    state    FL    1.0    NaN    NaN        NaN   NaN    NaN   NaN    NaN    NaN
17         dog    male  categorical    state    NY    2.0    NaN    NaN        NaN   NaN    NaN   NaN    NaN    NaN
18         cat  female  categorical  trained   yes    5.0    NaN    NaN        NaN   NaN    NaN   NaN    NaN    NaN
19         cat    male  categorical  trained    no    2.0    NaN    NaN        NaN   NaN    NaN   NaN    NaN    NaN
20         dog  female  categorical  trained    no    1.0    NaN    NaN        NaN   NaN    NaN   NaN    NaN    NaN
21         dog  female  categorical  trained   yes    1.0    NaN    NaN        NaN   NaN    NaN   NaN    NaN    NaN
22         dog    male  categorical  trained    no    4.0    NaN    NaN        NaN   NaN    NaN   NaN    NaN    NaN

In this dataframe, we have a lot of NaN values.

To get the count of NaN values for a specific column, I can do the following in my python code:

df["type"].isnull().sum()

#output: 15 

Hopefully this article has been useful for you to find the count of NaN values in a pandas dataframe using Python.

Other Articles You'll Also Like:

  • 1.  Format Numbers as Currency with Python
  • 2.  Open Multiple Files Using with open in Python
  • 3.  Python Check if Dictionary Value is Empty
  • 4.  math.degrees() Python – How to Convert Radians to Degrees in Python
  • 5.  pandas cumprod – Find Cumulative Product of Series or DataFrame
  • 6.  Using Python to Append Character to String Variable
  • 7.  How to Group By Columns and Find Maximum in pandas DataFrame
  • 8.  pandas mode – Find Mode of Series or Columns in DataFrame
  • 9.  How to Group By Columns and Count Rows in pandas DataFrame
  • 10.  Get Elapsed Time in Seconds in Python

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy

x