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:
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 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
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’sNone
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 –
- Pandas – Check if a DataFrame is Empty
- Pandas – Add an Empty Column to a DataFrame
- Pandas – Remove Spaces From Column Names
- Pandas – Select Columns of a Specific Type
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.