Check if a Cell Is Empty in Pandas

Check if a Cell Is Empty in Pandas

  1. Use the isnull() Function to Check if the Cell Is Empty
  2. Use the any() Function to Check if the Cell Is Empty
  3. Use the isnan Function to Check if the Cell Is Empty

Empty data is bad and not good when we need to compare, combine, or extract useful information, so knowing if they exist is important.

When we load our CSV data into Pandas as a dataframe, each piece of data is present within a cell, and every empty cell represents empty data. In this article, we will show you three approaches that we can use to check if a cell is empty in `Pandas.

Use the isnull() Function to Check if the Cell Is Empty

To showcase the functions in action, we will create a Pandas dataframe within some empty cells.

Code:

import pandas as pd
import numpy as np
df = pd.DataFrame([[1, 2, 3], [3, 4], [3, 4, 5, 6]], columns=list('ABCD'))
print(df)

Output:

A  B    C    D
0  1  2  3.0  NaN
1  3  4  NaN  NaN
2  3  4  5.0  6.0

Now that we have a dataframe with 3 empty cells, we can play with the isnull() function, which is designed to find missing values for array-like object - NaN, None, or NaT - and returns a Boolean value that indicates whether or not a missing value is present. You can apply the isnull() function on the entire dataframe, a specific column, or a specific cell.

To check across the dataframe or a specific column, we will pass the dataframe as the argument.

print(pd.isnull(df))
print(pd.isnull(df['C']))

Output:

A      B      C      D
0  False  False  False   True
1  False  False   True   True
2  False  False  False  False

0    False
1     True
2    False
Name: C, dtype: bool

The dataframe is shown with Boolean values that indicate whether there is an empty value or not, where False means a non-empty value and True means an empty value.

But more concerned with checking if a cell is empty, we can use the loc and iloc methods in conjunction with the isnull() method.

If we want to check the cell (index 1 at column C, which holds NaN), the following code will be appropriate to select and make the check.

Code:

print(pd.isnull(df.loc[1, 'C']))
print(pd.isnull(df['C'].iloc[1]))

Output:

True
True

Use the any() Function to Check if the Cell Is Empty

Another function that can allow us to check if any cell across the dataframe is empty is the any() function. As long as one cell is empty, the function returns True, and if otherwise, False.

We need to use the isnull() function and the values property to make it work.

Using the same dataframe from the previous section, the following code uses the isnull() function on the dataframe and now contains only Boolean values to indicate emptiness, and the Boolean values are accessible via the values property which can be chained to the any() function.

Code:

print(df.isnull().values.any())

Output:

True

Use the isnan Function to Check if the Cell Is Empty

We can store the index that has their cell empty in a column into a list by using the index and apply() functions as well as the test numpy.isnan.

We will select the column and then use the index function to achieve this. Within the index method, we select the same column again to use the apply() function with the test numpy.isnan.

These will be passed to the list() function.

Code:

list(df['D'].index[df['D'].apply(np.isnan)])

Output:

[0, 1]

With this, we have the index (0 and 1) of the empty cells within column D.

Olorunfemi Akinlua avatar Olorunfemi Akinlua avatar

Olorunfemi is a lover of technology and computers. In addition, I write technology and coding content for developers and hobbyists. When not working, I learn to design, among other things.

LinkedIn