How to check a cell is empty or nan in pandas DataFrames?

Today we will learn how to quickly find whether one or multiple cells in pandas DataFrame rows or columns contains empty values (Null, N/A, nan, None).

Create a dummy DataFrame

First off, we’ll create a very simple Data set that you can use in order to follow along with this example.

import pandas as pd
import numpy as np

prog_language = [np.nan, 'Javascript', 'Python', 'Java', 'Python', 'Java']
office = ['Bangkok', 'Osaka', 'Paris', None, pd.NA, 'Paris']
salary = [133.0, 136.0, 127.0, 185.0, 164.0, pd.NA]
data = dict(office=office, prog_language = prog_language, salary = salary)
test_df = pd.DataFrame(data=data)

Here’s our test data. Notice that some of the cells in the DataFrame contain null / NAN / none empty values:


Check if a specific DataFrame cell is empty

We can use the pandas functions isna or isnull and the loc or iloc accessors to determine whether a specific cell is empty:

if pd.isna(test_df.loc[2,'office']) is False:
    print("Your cell is empty.")
    print("Your cell is not empty.")

This will return the following:

Your cell is empty.

In a similar manner, using isnull and the indexer loc (iloc) accesor to test if our cell contains null values.

if pd.isnull(test_df.iloc[2,1]) is False:
    print("Your cell is empty.")
    print("Your cell is not empty.")

Check if cells are empty in a column

Let’s assume we would like to check if a column contains empty cells:

# define a column to search
my_col = test_df['prog_language']

This returns True.

Find if nan values exists in DataFrame

To check the entire DataFrame, we use a similar technique:


This returns a boolean Series, each value pertaining to each of our DataFrame columns.

office           True
prog_language    True
salary           True
dtype: bool

Note: this technique apply to whether we would like to check for NAN values in multiple columns or rows:

subset = test_df[['office', 'salary']]

# find rows with nulls

# find columns with nulls

Search for DataFrame rows containing nan values

If we want to find the rows in our DataFrame which contains any empty values, we can use the following snippet:




Related learning

How to import a text file into a Python string?