How To Check If Cell Is Empty In Pandas Dataframe
Last Updated :
27 Mar, 2025
An empty cell or missing value in the Pandas data frame is a cell that consists of no value, even a NaN or None. It is typically used to denote undefined or missing values in numerical arrays or DataFrames. Empty cells in a DataFrame can take several forms:
- NaN: Represents missing or undefined data.
- None: A Python object used to represent missing or undefined values.
- Empty strings (""): An empty text value.
- Zero values (0): Although not "empty" in a strict sense, zeros in numerical columns might be considered empty depending on the context
Using the isnull() method
isnull() is a method used to identify the cells in a data frame that contain missing values or undefined data that are represented by NaN (not a number) values. The function can't tell the difference between NaN values and empty cells.
We applied the isnull() function to check whether the data frame consists of NaN values, and it outputs the data frame with boolean values: true if the value in the cell is empty or NaN, and false if the cell contains a value.
Python
import pandas as pd
import numpy as np
# Create a sample DataFrame
df = pd.DataFrame({'col1': [1, 2, None], 'col2': [3, None, 5]})
df.isnull()
Output:
col1 col2
0 False False
1 False True
2 True False
Explanation: This code creates a Pandas DataFrame df with two columns (col1 and col2) containing some None (null) values. The isnull() method is then called on the DataFrame, which returns a new DataFrame of the same shape, with True for the cells that are None (null) and False for the non-null cells.
Using isna() method
isna() is a method in Pandas similar to the isnull() method and gives the same result where it detects missing or undefined values within the data frame.
The difference between isna() and is_null() methods is their naming, isna() is an alias for isnull(). Both methods can be used interchangeably to achieve the same outcome.
Here, we are creating a new variable for saving the values that we get by applying the isna() method to the data frame and printing them.
Python
import pandas as pd
import numpy as np
# Create a sample DataFrame
df = pd.DataFrame({'col1': [1, 2, None], 'col2': [3, None, 5]})
# Check for NaN values using isna()
na_df = df.isna()
print(na_df)
Output:
col1 col2
0 False False
1 False True
2 True False
Explanation: This code creates a Pandas DataFrame df with two columns (col1 and col2) that contain some None (null) values. The isna() method is then called on the DataFrame, which returns a new DataFrame of the same shape, with True indicating the presence of NaN values (missing values) and False indicating non-missing values.
Checking for empty cells explicitly
We can identify the rows or columns that are empty using the all() function along with the is_null() function.
Python
import pandas as pd
import numpy as np
# Create a sample DataFrame with null values
data = {'A': [1, 2, np.nan, 4],
'B': [5, np.nan, np.nan, 8],
'C': [np.nan, np.nan, np.nan, np.nan]}
df = pd.DataFrame(data)
print(df)
Output:
A B C
0 1.0 5.0 NaN
1 2.0 NaN NaN
2 NaN NaN NaN
3 4.0 8.0 NaN
Explanation: This code creates a Pandas DataFrame df with three columns (A, B, C) and four rows, where some of the cells contain NaN values (representing missing data). The np.nan is used to represent missing or undefined values in the DataFrame. Finally, it prints the DataFrame to the console.
Check for empty cells using boolean indexing
Here we are checking whether our dataset contains any empty rows. 'axis=1' will check if all values along axis 0 (i.e., along rows) in each row are True. If all values in a row are True, it means that all cells in that row are null.
Python
import pandas as pd
import numpy as np
# Create a sample DataFrame
df = pd.DataFrame({'col1': [1, 2, None], 'col2': [3, None, 5]})
empty_rows = df.isnull().all(axis=1)
empty_rows
Output:
0 False
1 False
2 True
3 False
dtype: bool
Explanation: Here we are checking whether our dataset contains any empty columns. We are using the loc() function to select rows and columns based on their labels (index names and column names). 'axis=0' will check if all values along axis 0 (i.e., along columns) in each column are True. If all values in a column are True, it means that all cells in that column are null.
Python
# Check for empty cells using boolean indexing along columns (axis=1)
empty_columns = df.isnull().all(axis=0)
print(empty_columns)
Output:
A False
B False
C True
dtype: bool
Similar Reads
How to check dataframe is empty in Scala? In this article, we will learn how to check dataframe is empty or not in Scala. we can check if a DataFrame is empty by using the isEmpty method or by checking the count of rows. Syntax: val isEmpty = dataframe.isEmpty OR, val isEmpty = dataframe.count() == 0 Here's how you can do it: Example #1: us
2 min read
How to Check if PySpark DataFrame is empty? In this article, we are going to check if the Pyspark DataFrame or Dataset is Empty or Not. At first, let's create a dataframe Python3 # import modules from pyspark.sql import SparkSession from pyspark.sql.types import StructType, StructField, StringType # defining schema schema = StructType([ Struc
1 min read
How to check if a csv file is empty in pandas Reading CSV (Comma-Separated Values) files is a common step in working with data, but what if the CSV file is empty? Python script errors and unusual behavior can result from trying to read an empty file. In this article, we'll look at methods for determining whether a CSV file is empty before attem
4 min read
Create empty dataframe in Pandas The Pandas Dataframe is a structure that has data in the 2D format and labels with it. DataFrames are widely used in data science, machine learning, and other such places. DataFrames are the same as SQL tables or Excel sheets but these are faster in use.Empty DataFrame could be created with the help
1 min read
Check if dataframe contains infinity in Python - Pandas When working with data, you may encounter infinity values (positive or negative), represented as np.inf and -np.inf in Python using the NumPy library. It's important to check if your Pandas DataFrame contains any infinity values before proceeding with analysis. Let's explore different methods to det
3 min read
Check missing dates in Pandas In this article, we will learn how to check missing dates in Pandas. A data frame is created from a dictionary of lists using pd.DataFrame() which accepts the data as its parameter. Note that here, the dictionary consists of two lists named Date and Name. Both of them are of the same length and some
4 min read