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?
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.
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.
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 –
- Drop Rows with NaNs in Pandas DataFrame
- Pandas – Get Columns with Missing Values
- Pandas – Get dataframe summary with info()
- Missing Values in Pandas Category Column
- Pandas – Percentage of Missing Values in Each Column
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.