The Python pandas function DataFrame.isna() helps users identify missing data (NaN or None) in a DataFrame. This can be especially useful for seeing if data needs to be cleaned up before beginning analysis.

Web Hosting
Fast, scalable hosting for any website
  • 99.9% uptime
  • PHP 8.3 with JIT compiler
  • SSL, DDoS protection, and backups

What is the syntax for pandas isna()?

Since pandas isna() doesn’t take any parameters, its syntax is quite straightforward:

DataFrame.isna()
python

How to use the pandas isna() function

When isna() is applied to a DataFrame, it creates a new DataFrame with Boolean values. If a value in the original DataFrame is missing (e.g., marked as NaN or None), isna() will show True where the value is located. Otherwise, the function will display False.

Note

If, in addition to identifying NaN or None values, you also want to remove them, check out the pandas dropna() function. If you don’t want to remove these values, but instead systematically replace them, the fillna() function is a useful tool for doing so.

Identifying missing values in a DataFrame

The following example uses a DataFrame with data about different individuals, where some information is missing.

import pandas as pd
# Create DataFrame example
data = {
    'Name': ['Alice', 'Bob', None, 'David'],
    'Age': [25, None, 35, 40],
    'City': ['New York', 'Los Angeles', 'Chicago', None] 
}
df = pd.DataFrame(data)
print(df)
python

The DataFrame looks like this:

Name   Age         City
0  Alice  25.0     New York
1    Bob   NaN  Los Angeles
2   None  35.0      Chicago
3  David  40.0         None

The information that is missing has been marked as None or NaN. To see exactly which values are missing, you can call isna() on the DataFrame.

# Applying  pandas isna()
missing_values = df.isna()
print(missing_values)
python

The function call returns a new DataFrame, where missing values from the original data are marked as True, while values that are present are marked as False. Here’s the output:

Name    Age   City
0  False  False  False
1  False   True  False
2   True  False  False
3  False  False   True

Counting the amount of missing values per column

It can also be useful to know how many values are missing in each column to help you decide how to handle them. You can use isna() along with Python’s sum() function to count the number of missing values in each column.

# Count missing values per column
missing_count = df.isna().sum()
print(missing_count)
python

This shows you the number of missing values in each column:

Name     1
Age      1
City     1
dtype: int64
Was this article helpful?
Go to Main Menu