How to count number of NaN values in Pandas?
Last Updated :
15 Nov, 2024
Let’s discuss how to count the number of NaN values in Pandas DataFrame. In Pandas, NaN (Not a Number) values represent missing data in a DataFrame.
Counting NaN values of Each Column of Pandas DataFrame
To find the number of missing (NaN) values in each column, use the isnull() function followed by sum(). This will provide a count of NaN values per column.
Python
import pandas as pd
import numpy as np
# Example dataset
data = {
'A': [1, 2, np.nan, 4],
'B': [np.nan, 2, np.nan, 3],
'C': [1, np.nan, np.nan, np.nan]
}
df = pd.DataFrame(data)
# Count NaNs in each column
column_nan_count = df.isnull().sum()
print("NaN count per column:")
print(column_nan_count)
Output:

Pandas DataFrame with NaN Values
NaN count per column:
A 1
B 2
C 3
dtype: int64
Counting NaN Values of Specific Rows
To count NaNs in specific rows, use loc
or iloc to select the row and then call isnull().sum()
.
Python
# Count NaNs in the first row
row_nan_count = df.iloc[0].isnull().sum()
print("NaN count in the first row:", row_nan_count)
Output:
NaN count in the first row: 1
Counting NaN Values in the Entire DataFrame
To get the total count of NaN values across the entire DataFrame, use isnull().sum().sum()
. This performs a summation of NaNs per column, then sums these totals to get an overall count.
Python
# Count total NaNs in the DataFrame
total_nan_count = df.isnull().sum().sum()
print("Total NaN count:", total_nan_count)
Output:
Total NaN count: 6
Using isna()
as an Alternative
The isna()
function works similarly to isnull()
for detecting NaN values, and you can use it interchangeably for the same results.
Python
# Using isna() to count NaNs in each column
column_nan_count_isna = df.isna().sum()
print("NaN count per column using isna():")
print(column_nan_count_isna)
Output:
NaN count per column using isna():
A 1
B 2
C 3
dtype: int64
Using describe() to find non-NaN Values in Each Column
The describe()
method provides a quick overview of each column, including the non-NaN count. Subtracting this count from the total number of rows can give you the NaN count.
Python
# Using describe() for additional insights
non_nan_count = df.describe().loc['count']
nan_count_using_describe = len(df) - non_nan_count
print("NaN count per column using describe():")
print(nan_count_using_describe)
Output:
NaN count per column using describe():
A 1.0
B 2.0
C 3.0
Name: count, dtype: float64
This approach provides flexibility for deciding whether to drop rows, drop columns, or fill missing values based on the proportion of NaNs in each feature.
Identifying Rows or Columns with NaN Values
Sometimes you might need to identify which rows or columns contain any NaN values, rather than counting them.
1. Check for Columns with Any NaN values
To check for columns that contain at least one NaN value, use isna().any() on the DataFrame
Python
columns_with_nan = df.isna().any()
print("Columns with NaN values:")
print(columns_with_nan)
Output:
Columns with NaN values:
A True
B True
C True
dtype: bool
2. Check for Rows with any NaN Values
To check for rows that contain NaNs, use isna().any(axis=1), which checks along the row axis.
Python
rows_with_nan = df.isna().any(axis=1)
print("Rows with NaN values")
print(rows_with_nan)
Output:
Rows with NaN values
0 True
1 True
2 True
3 True
dtype: bool
Knowing how to count and locate NaNs in your data is essential for cleaning and preprocessing.
Similar Reads
How to Plot Value Counts in Pandas
In this article, we'll learn how to plot value counts using provide, which can help us quickly understand the frequency distribution of values in a dataset. Table of Content Concepts Related to Plotting Value CountsSteps to Plot Value Counts in Pandas1. Install Required Libraries2. Import Required L
3 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
3 min read
How to Count Occurrences of Specific Value in Pandas Column?
Let's learn how to count occurrences of a specific value in columns within a Pandas DataFrame using .value_counts() method and conditional filtering. Count Occurrences of Specific Values using value_counts()To count occurrences of values in a Pandas DataFrame, use the value_counts() method. This fun
4 min read
Pandas Index.value_counts()-Python
Python is popular for data analysis thanks to its powerful libraries and Pandas is one of the best. It makes working with data simple and efficient. The Index.value_counts() function in Pandas returns the count of each unique value in an Index, sorted in descending order so the most frequent item co
3 min read
How to count unique values in a Pandas Groupby object?
Here, we can count the unique values in Pandas groupby object using different methods. This article depicts how the count of unique values of some attribute in a data frame can be retrieved using Pandas. Method 1: Count unique values using nunique() The Pandas dataframe.nunique() function returns a
3 min read
Counting Frequency of Values by Date in Pandas
Counting the frequency of values by date is a common task in time-series analysis, where we need to analyze how often certain events occur within specific time frames. Understanding these frequencies can provide valuable insights if we analyze sales data, website traffic, or any other date-related d
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
2 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. 1. DataFrame.isnull() MethodDataFrame.isnull() function detect missing values in the given object. It return a boolean same-sized object indicating if the values are
3 min read
Count Values in Pandas Dataframe
Counting values in Pandas dataframe is important for understanding the distribution of data, checking for missing values or summarizing data. In this article, we will learn various methods to count values in a Pandas DataFrame We will be using below dataframe to learn about various methods: [GFGTABS
3 min read
Count number of rows and columns in Pandas dataframe
In Pandas understanding number of rows and columns in a DataFrame is important for knowing structure of our dataset. Whether we're cleaning the data, performing calculations or visualizing results finding shape of the DataFrame is one of the initial steps. In this article, we'll explore various ways
3 min read