check if a cell is None in a pandas dataframe

Check if a Cell in Pandas DataFrame is None or an empty string

In this tutorial, we will learn how to check if a cell is a None or an empty string. A good exposure to Python is recommended but not required.

How to check if a cell of a dataframe is an empty string or None?

To check if a cell is None, we can compare the cell’s value with Python’s None type using the None identifier.

cell = df.iloc[index, column]
is_cell_null = (cell == None)

Here,

  • df – A Pandas DataFrame object.
  • df.iloc – Slicing property of Pandas DataFrame. It is used to fetch cells, rows, and columns.

In the above code, we used iloc property of Pandas DataFrame to obtain the value contained in the cell. The location of the cell in a dataframe is determined by its index and column. We compared this value with the None type using the None identifier in Python. If the comparison returns True, then the cell is empty, or equivalently, it’s None.

To determine if a cell contains an empty string, we can extract the value from the cell as above and then compare it with an empty string. This can be done as follows:

cell = df.iloc[index, column]
is_cell_empty_str = (cell.strip() == '')

Here, we extracted the value contained in the cell using iloc and then we compared this value with an empty string, rather than comparing it with the None type as before. We used the strip() method of Python string objects to remove all empty spaces, if any, from the string contained in the cell. We did this because the data we have can possibly contain strings with spaces only.

Examples

Let’s understand the above code with some examples. For our examples, we create a dataframe for weather data in a city. For simplicity, we restrict our entries to a week.

import pandas as pd

#Create data for dataframe
d = {
    "Day" : ["Sunday", " ", None, "Wednesday", "Thursday", "Friday", "Saturday"],
    "Max. Temp." : [41.7, 36.5, 37.7, 35.9, 37.2, 40.2, 42.3],
    "Min. Temp." : [28.8, 26.1, 26.6, 33.1, 26.5, 28.8, 29.6],
    }

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

#Print the dataframe
print(df)

Output:

📚 Data Science Programs By Skill Level

Introductory

Intermediate ⭐⭐⭐

Advanced ⭐⭐⭐⭐⭐

🔎 Find Data Science Programs 👨‍💻 111,889 already enrolled

Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.

         Day  Max. Temp.  Min. Temp.
0     Sunday        41.7        28.8
1                   36.5        26.1
2       None        37.7        26.6
3  Wednesday        35.9        33.1
4   Thursday        37.2        26.5
5     Friday        40.2        28.8
6   Saturday        42.3        29.6

As you can see in the output, we have one value each for the None value and an empty string in the column ‘Day’.

Example 1: Check if a cell contains None value

check if a cell is None in a pandas dataframe

To check if a cell contains a None value, we compare the value contained in the cell with None using the == operator. Let’s do this for a cell containing the None value.

is_cell_null = (df.iloc[2, 0] == None)
print(is_cell_null)

Output:

True

The output is True for the cell containing a None value. Now we will apply the same function to a cell that doesn’t contain a None value.

is_cell_null = (df.iloc[6, 0] == None)
print(is_cell_null)

Output:

False

As we expected, the output is False.

Example 2: Check if a cell contains an empty string.

To check if a cell contains an empty string, we compare the value contained in the cell with an empty string. Let’s do this for a cell containing an empty string.

is_cell_empty_str = (df.iloc[1, 0].strip() == '')
print(is_cell_empty_str)

Output:

True

The output is True, which means that the cell contains an empty string. This is what we expected the output to be. Now, let’s run the same code for a cell that doesn’t contain an empty string.

is_cell_empty_str = (df.iloc[6, 0].strip() == '')
print(is_cell_empty_str)

Output:

False

As expected, the output is False.

Summary

From this tutorial, we have learned to:

  • Check if a cell contains a None value using Python’s None identifier and the equality operator.
  • Using a dummy string and the strip() method of Python string objects, determine whether a cell contains an empty string or a string with only white spaces.

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.


Author

Scroll to Top