We might need to count the number of NaN values for each feature in the dataset so that we can decide how to deal with it. For example, if the number of missing values is quite low, then we may choose to drop those observations; or there might be a column where a lot of entries are missing, so we can decide whether to include that variable at all.
Method 1: Using describe()
We can use the describe() method which returns a table containing details about the dataset. The count property directly gives the count of non-NaN values in each column. So, we can get the count of NaN values, if we know the total number of observations.
Method 2: Using sum()
The isnull() function returns a dataset containing True and False values. Since, True is treated as a 1 and False as 0, calling the sum() method on the isnull() series returns the count of True values which actually corresponds to the number of NaN values.
Counting NaN in a column :
We can simply find the null values in the desired column, then get the sum.
Counting NaN in a row :
Counting NaN in the entire DataFrame :
To count NaN in the entire dataset, we just need to call the sum() function twice – once for getting the count in each column and again for finding the total sum of all the columns.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
- Count NaN or missing values in Pandas DataFrame
- Count the NaN values in one or more columns in Pandas DataFrame
- Python | Visualize missing values (NaN) values using Missingno Library
- How to Drop Rows with NaN Values in Pandas DataFrame?
- Ways to Create NaN Values in Pandas DataFrame
- Drop rows from Pandas dataframe with missing values or NaN in columns
- Replace NaN Values with Zeros in Pandas DataFrame
- Replace all the NaN values with Zero's in a column of a Pandas dataframe
- Highlight the nan values in Pandas Dataframe
- How to Drop Columns with NaN Values in Pandas DataFrame?
- Python | Replace NaN values with average of columns
- How to remove NaN values from a given NumPy array?
- Check for NaN in Pandas DataFrame
- Python | cmath.nan Constant
- Mapping external values to dataframe values in Pandas
- Highlight the negative values red and positive values black in Pandas Dataframe
- How to Count Distinct Values of a Pandas Dataframe Column?
- Python - Extract Unique values dictionary values
- Python - Remove duplicate values across Dictionary Values
- Python - Extract ith column values from jth column values
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.