How to Check if a Number is NaN in Python?

In this tutorial, I will explain how to check if a number is NaN in Python. As a data scientist working for a US-based company, I often need to handle and clean large datasets, where I had a requirement to check if certain numbers are NaN (Not a Number). Let’s explore various methods to achieve this in Python with examples and screenshots of executed example code.

NaN in Python

NaN stands for “Not a Number.” It is a special floating-point value defined by the IEEE 754 standard, used to represent undefined or unrepresentable values, such as the result of 0/0. In Python, NaN values can be a common occurrence when dealing with data from external sources like databases, CSV files, or APIs.

Read How to Generate Random Numbers Between 0 and 1 in Python?

Methods to Check for NaN in Python

There are several ways to check if a number is NaN in Python. Let us learn some important methods with examples.

Method 1. Use the math.isnan() Function

The math module in Python provides the isnan() function, which can be used to check if a value is NaN. This function returns True if the value is NaN and False otherwise. Here’s an example:

import math

# Example values
value1 = float('nan')
value2 = 3.14

# Check for NaN
print(math.isnan(value1))  
print(math.isnan(value2))

Output:

True
False

I have executed the above example code and added the screenshot below.

Check if a Number is NaN in Python

This method is easy and works well for individual floating-point values.

Check out How to Convert Decimal Numbers to Binary in Python?

Method 2. Use the numpy.isnan() Function

If you’re working with arrays or large datasets, the NumPy library offers a more efficient way to check for NaN values using the numpy.isnan() function. NumPy is widely used in the data science community for numerical operations.

import numpy as np

# Example array
data = np.array([1.0, 2.5, np.nan, 4.8, np.nan])

# Check for NaN
nan_mask = np.isnan(data)
print(nan_mask)  

Output:

[False False  True False  True]

I have executed the above example code and added the screenshot below.

How to Check if a Number is NaN in Python

This method is particularly useful when dealing with large datasets, as it allows you to quickly identify NaN values across the entire array.

Read How to Round Numbers to 2 Decimal Places in Python?

Method 3. Use Pandas for DataFrames

Pandas is another powerful library widely used for data manipulation and analysis. It provides several functions to check for NaN values in DataFrames and Series.

1. Use pd.isna() and pd.isnull()

The pd.isna() and pd.isnull() functions are interchangeable and can be used to detect NaN values in Pandas objects.

import pandas as pd

# Example DataFrame
data = {
    'Name': ['Alice', 'Bob', 'Charlie', 'David'],
    'Age': [24, np.nan, 30, np.nan]
}
df = pd.DataFrame(data)

# Check for NaN
print(pd.isna(df))

Output:

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

I have executed the above example code and added the screenshot below.

Check if a Number is NaN in Python Pandas

2. Use DataFrame.isna() and DataFrame.isnull()

These methods can be called directly on DataFrame objects to check for NaN values.

# Check for NaN in DataFrame
print(df.isna())

Both methods will return a DataFrame of the same shape, True indicating the presence of NaN values.

Check out How to Generate Credit Card Numbers in Python for Testing?

Handle NaN Values

Once you’ve identified NaN values in your dataset, the next step is to handle them appropriately. Here are some common strategies:

1. Remove NaN Values

You can remove rows or columns containing NaN values using the dropna() method in Pandas.

# Remove rows with NaN values
df_cleaned = df.dropna()
print(df_cleaned)

2. Fill NaN Values

Alternatively, you can fill NaN values with a specified value using the fillna() method.

# Fill NaN values with a specific value
df_filled = df.fillna(0)
print(df_filled)

You can also fill NaN values with the mean, median, or mode of the column.

# Fill NaN values with the mean of the column
df['Age'] = df['Age'].fillna(df['Age'].mean())
print(df)

Check out How to Convert Letters to Numbers in Python?

Example: Data Cleaning for a Sales Dataset

Let’s consider a real-world scenario where we have a sales dataset from a retail store in Los Angeles. The dataset contains information about sales transactions, including the product name, quantity sold, and sale price. However, some entries have missing values, which are represented as NaN.

# Example sales dataset
sales_data = {
    'Product': ['Laptop', 'Smartphone', 'Tablet', 'Smartwatch', 'Laptop'],
    'Quantity': [10, np.nan, 5, np.nan, 7],
    'Price': [1000, 800, 300, 200, np.nan]
}
sales_df = pd.DataFrame(sales_data)

# Check for NaN values
print(sales_df.isna())

# Handle NaN values
# Fill NaN in 'Quantity' with the mean quantity
sales_df['Quantity'] = sales_df['Quantity'].fillna(sales_df['Quantity'].mean())

# Fill NaN in 'Price' with the median price
sales_df['Price'] = sales_df['Price'].fillna(sales_df['Price'].median())

print(sales_df)

By handling NaN values appropriately, we ensure that our sales analysis is accurate and reliable.

Read How to Pad Numbers with Leading Zeros in Python?

Conclusion

In this tutorial, I explained how to check if a number is NaN in Python. I covered math.isnan() function, numpy.isnan() function,pd.isna() and pd.isnull() in Pandas and DataFrame.isna() and DataFrame.isnull() in DataFrame. I also covered how to handle NaN values and a real-world example to clean data for a sales dataset.

You may also like to read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.