Pandas dataframe stores values in a row and column format, and some data may be missing in the dataset.
You can count NaN values in Pandas dataframe using the df.isna() method.
Basic Example
df['Column 1'].isna().sum()
Output
3
NaN
values are also known as missing values. It is also denoted asNone
.
This tutorial teaches you how to count NaN values in the Pandas dataframe.
Sample Dataframe
To demonstrate the counting of NaN
values, first, create a dataframe with the NaN
values.
There are three columns, and each column contains a few NaN
values.
import pandas as pd
import numpy as np
data = {'Column 1': [1,2,np.nan,4,5,np.nan,None],
'Column 2': [1,2,np.nan,4,np.nan,np.nan,None],
'Column 3': [1,2,None,4,5,None,None]
}
df = pd.DataFrame(data,columns=['Column 1','Column 2','Column 3'])
df
Dataframe Will Look Like
Column 1 | Column 2 | Column 3 | |
---|---|---|---|
0 | 1.0 | 1.0 | 1.0 |
1 | 2.0 | 2.0 | 2.0 |
2 | NaN | NaN | NaN |
3 | 4.0 | 4.0 | 4.0 |
4 | 5.0 | NaN | 5.0 |
5 | NaN | NaN | NaN |
6 | NaN | NaN | NaN |
Now, you’ll use this dataframe and count the NaN
values.
Count Nan Values in Column
The isna()
method returns the same sized boolean object indicating if the item is missing value or not.
- sum the object using the
sum()
function to get the total number of missing values
Code
df['Column 1'].isna().sum()
Output
3
Count Nan Values in Multiple Columns
In this section, you’ll count the NaN
values in a Multiple columns using the isna() method.
- Pass the columns as a list to the
isna()
method. - It returns the same sized boolean object indicating if the item is missing value or not.
- Sum the object using the
sum()
method to get the total number of missing values
Code
df[['Column 1', 'Column 2']].isna().sum()
Output
Column 1 3
Column 2 4
dtype: int64
Count NaN Values in Every Column Of Dataframe
In this section, you’ll count the NaN
values in each column the isna() method.
- Call the
isna()
method in the dataframe object. - It returns the same sized boolean object indicating if the item is missing value or not.
- Sum the object using the
sum()
function to get the total number of missing values
Code
df.isna().sum()
Output
Column 1 3
Column 2 4
Column 3 3
dtype: int64
Count NaN values in Entire Dataframe
In this section, you’ll count the NaN
values in entire dataframe using the isna() method.
- Call the
isna()
method in the dataframe object. - It returns the same sized boolean object indicating if the item is missing value or not.
- Sum the object to get the total number of missing values in each column and again invoke the
sum()
function to count the total number of missing values
Code
df.isna().sum().sum()
Output
10
Count Nan Value in a specific row
In this section, you’ll learn how to count the NaN values in a specific row of the dataframe.
- Select the desired row of the dataframe using the
loc
attribute - use the
isna()
method andsum()
to count the missing values. - It’ll return the missing values in each column.
- Again invoke the
sum()
function to calculate the totalNaN
values in the complete row.
Code
df.loc[[4]].isna().sum().sum()
Output
1
Count Rows with Nan Values
In this section, you’ll learn how to count the number of rows with NaN
values.
- Use the
isna()
method to check if the value is missing - Use the
any(axis=1)
method to check if any of the values are missing on axis 1. Axis 1 denotes the row axis - Use the
sum()
function to calculate the total number of rows withNaN
values
Code
df.isna().any(axis=1).sum()
You’ll see output 4 as four rows in the dataframe contains missing values.
Output
4