Skip to Content

Check if a cell in Pandas DataFrame is NaN

In this tutorial, you will learn to check if a cell in a dataframe has a NaN value or not. It is recommended that you have good exposure to Python, but this is not necessary.

How to check if a dataframe’s cell has a NaN value?

check if a pandas dataframe cell is nan or not

In Pandas, all numerical values on which mathematical operations cannot be applied are represented as NaN, in addition to empty cells. These values can be Python’s inf or None, and NumPy’s np.nan. To check if a cell has a NaN value, we can use Pandas’ inbuilt function isnull(). The syntax is-

cell = df.iloc[index, column]
is_cell_nan = pd.isnull(cell)

Here,

  • df – A Pandas DataFrame object.
  • df.iloc – A dataframe’s property to extract a cell, a row, or a column.
  • pd.isnull() – Pandas inbuilt function to check if a value passed to it is null or not.

Here, we have extracted the cell’s value by passing the index and column of that cell to df.iloc. Then we passed the obtained value to the isnull() function. This function returns True if the cell contains a NaN value; else, it returns False.

Examples

Let’s understand the above syntax with some examples. For our examples, we create a sample dataframe for the sale of items at a store for a week.

import pandas as pd
import numpy as np

#Create data for dataframe
d = {
    "Product" : ["Chilli Powder (100 g)", "Mayonnaise", "Wheat Flour (25 kg)", "Rice (25 kg)", "Salt (500g)"],
    "Price/unit" : [10, 120, 450, 1100, 22],
    "Units sold" : [55, np.nan, 23, 27, np.nan],
    }

#Create the dataframe
df = pd.DataFrame(d)

#Print the dataframe
print(df)

Output:

                 Product  Price/unit  Units sold
0  Chilli Powder (100 g)          10        55.0
1             Mayonnaise         120         NaN
2    Wheat Flour (25 kg)         450        23.0
3           Rice (25 kg)        1100        27.0
4            Salt (500g)          22         NaN

In the data, there are two nan values. You can notice that they are represented as NaN in the above output. We will use these nan values for the demonstrations.

Example 1: Apply isnull() function on a cell containing nan value

To check if a cell contains a null value, we first obtain the cell value using iloc. Then we pass this value to the isnull() function. Now, we will use the cell with the column ‘Units sold’ and index 1. Note that it has a nan value.

cell = df.iloc[1, 2]
is_cell_nan = pd.isnull(cell)
print(is_cell_nan)

Output:

True

Since the cell had a nan value, the isnull() function returns True, which is seen in the output.

Example 2: Apply isnull() function on a cell which doesn’t contain nan value

Now let’s apply the isnull() function on the cell with column ‘Units sold’ and index 2. This cell doesn’t have a nan value.

cell = df.iloc[2, 2]
is_cell_nan = pd.isnull(cell)
print(is_cell_nan)

Output:

False

The isnull() function returns False in this case, as was expected.

Summary

In this tutorial, we have learned the following:

  • Nan, none, and inf are represented as NaN values in Pandas.
  • Use Pandas inbuilt function isnull() to check if a cell contains a nan value or not.

You might also be interested in –


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.