Home »
Python »
Python Programs
How to check whether a Pandas DataFrame is empty?
Given a DataFrame, we have to check whether a Pandas DataFrame is empty.
Submitted by Pranit Sharma, on April 29, 2022
Pandas is a special tool that allows us to perform complex manipulations of data effectively and efficiently. Inside pandas, we mostly deal with a dataset in the form of DataFrame. DataFrames are 2-dimensional data structures in pandas. DataFrames consists of rows, columns, and the data. A certain operation can be performed on DataFrames.
Problem statement
Given a DataFrame, we have to check whether a Pandas DataFrame is empty.
Checking whether a Pandas DataFrame is empty
In pandas, we can check if a DataFrame is empty or not by using the following syntax:
DataFrame.empty()
By using the above syntax, we will get a Boolean value i.e., True if the DataFrame is empty and False if a DataFrame is not empty.
Note
To work with pandas, we need to import pandas package first, below is the syntax:
import pandas as pd
Let us understand with the help of an example.
Python program to check whether a Pandas DataFrame is empty
# Importing pandas package
import pandas as pd
# Creating an Empty dictionary
d = {}
# Creating an Empty DataFrame first
df=pd.DataFrame(d)
# Check if DataFrame is empty or
# not DataFrame
result = df.empty
# Display result
print(result)
Output
The output of the above program is:
True
Python Pandas Programs »