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
Non-linear Components
In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
Class Diagram | Unified Modeling Language (UML)
A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
Spring Boot Tutorial
Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Backpropagation in Neural Network
Backpropagation is also known as "Backward Propagation of Errors" and it is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network. In this article we will explore what
10 min read
AVL Tree Data Structure
An AVL tree defined as a self-balancing Binary Search Tree (BST) where the difference between heights of left and right subtrees for any node cannot be more than one. The absolute difference between the heights of the left subtree and the right subtree for any node is known as the balance factor of
4 min read
What is Vacuum Circuit Breaker?
A vacuum circuit breaker is a type of breaker that utilizes a vacuum as the medium to extinguish electrical arcs. Within this circuit breaker, there is a vacuum interrupter that houses the stationary and mobile contacts in a permanently sealed enclosure. When the contacts are separated in a high vac
13 min read
Polymorphism in Java
Polymorphism in Java is one of the core concepts in object-oriented programming (OOP) that allows objects to behave differently based on their specific class type. The word polymorphism means having many forms, and it comes from the Greek words poly (many) and morph (forms), this means one entity ca
7 min read
3-Phase Inverter
An inverter is a fundamental electrical device designed primarily for the conversion of direct current into alternating current . This versatile device , also known as a variable frequency drive , plays a vital role in a wide range of applications , including variable frequency drives and high power
13 min read
Random Forest Algorithm in Machine Learning
A Random Forest is a collection of decision trees that work together to make predictions. In this article, we'll explain how the Random Forest algorithm works and how to use it.Understanding Intuition for Random Forest AlgorithmRandom Forest algorithm is a powerful tree learning technique in Machine
7 min read
What is a Neural Network?
Neural networks are machine learning models that mimic the complex functions of the human brain. These models consist of interconnected nodes or neurons that process data, learn patterns, and enable tasks such as pattern recognition and decision-making.In this article, we will explore the fundamenta
14 min read