Knowing how to check if a dataframe is empty or not can be quite handy when working with dataframes in Pandas. In this tutorial, we will look at some of the ways to check if a pandas dataframe is empty or not with the help of some examples.
If you prefer a video tutorial over text, check out the following video detailing the steps in this tutorial –
What does it mean for a dataframe to be empty in pandas?
Before we proceed with knowing how to check if a dataframe is empty or not, let’s first define what it means for a dataframe to be empty. Generally, a dataframe is considered empty if it does not have any data. That is, there are no rows present in the dataframe which is what several methods mentioned in this tutorial check for.
How to check whether a dataframe is empty?
To check if a dataframe is empty, you can use the dataframe’s empty
property or you can check if the number of rows is zero using its shape
property (shape[0]
gives the row count) or the len()
function. The following is the syntax:
# using .empty property df.empty # using shape[0] df.shape[0] == 0 # using len() function len(df) == 0 # using len() function len(df.index) == 0
Note that if a dataframe contains only NaNs, it is still not considered empty. Let’s now have a look at the usage of these methods with the help of examples. Let’s first create an empty dataframe that we will be using throughout this tutorial.
import pandas as pd # create an empty dataframe df = pd.DataFrame() # display the dataframe print(df)
Output:
Empty DataFrame Columns: [] Index: []
Method 1 – Using df.empty
property
This property of the dataframe returns True
if the dataframe is empty and False
if it’s not.
# using df.empty property print(df.empty)
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.
True
We get True
as the output since the dataframe is empty. Let’s see what we get if the dataframe contains only NaN values.
import numpy as np # create dataframe of NaN values df_nan = pd.DataFrame({'Col1': [np.nan]}) # check if dataframe is empty print(df_nan.empty)
Output:
False
You can see that we get False
as the output since the dataframe is not considered empty even if it only has NaN values.
Method 2 – Using df.shape[0]
The dataframe’s shape gives us a tuple of the number of rows and columns of the dataframe, (row_count, column_count). Hence, df.shape[0]
represents the count of rows in the dataframe. If this count is 0, then we consider the dataframe to be empty.
# using df.shape[0] print(df.shape[0] == 0)
Output:
True
We get True
as the output since the dataframe is empty.
Method 3 – Using len(df)
or len(df.index)
len()
is a built-in function in python that gives the length (or the number of items) of a python object. If the length of a pandas dataframe or its index is zero, we can say that the dataframe is empty.
# using len(df) print(len(df) == 0) # using len(df.index) print(len(df.index) == 0)
Output:
True True
We get True
as the output in both cases.
Now that you have looked at how to check if a pandas dataframe is empty, can you tell if the following dataframe is empty or not?
import numpy as np df_nan = pd.DataFrame({'Col1': [np.nan]}) df_nan = df_nan.dropna() # show the dataframe print(df_nan)
Output:
Empty DataFrame Columns: [Col1] Index: []
Here, we created a dataframe of one column containing only NaN and then use the pandas dropna()
function to drop all the NaN values in the dataframe. Is this dataframe empty? Well, let’s see for ourselves.
print(df_nan.empty) print(df_nan.shape[0]==0) print(len(df_nan)==0) print(len(df_nan.index)==0)
Output:
True True True True
We find that the dataframe is empty. This is because even though the dataframe has a column, there are no values present in the dataframe.
Summary
In this tutorial, we looked at how to check if a pandas dataframe is empty or not. The following are the key takeaways.
- We say a dataframe is empty if it does not have any data (that is, no rows). Note that if a dataframe has rows with
NaN
orNone
values it will not be considered empty. - You can use the following methods to check if a pandas dataframe is empty –
- Using the pandas dataframe
empty
property. - Checking if the number of rows is 0 or not. You can use
shape[0]
or thelen()
function to get the number of rows in a dataframe.
- Using the pandas dataframe
For more on the pandas empty
property, refer to its documentation.
With this, we come to the end of this tutorial. The code examples and results presented in this tutorial have been implemented in a Jupyter Notebook with a python (version 3.8.3) kernel having numpy version 1.18.5 and pandas version 1.0.5
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.
Tutorials on common dataframe operations in pandas –
- Reset Index in Pandas – With Examples
- Pandas – Sort a DataFrame
- Pandas – Filter DataFrame for multiple conditions
- Apply a Function to a Pandas DataFrame
- Compare Two DataFrames for Equality in Pandas
- Pandas – Check if a DataFrame is Empty
- Create a Line Plot from Pandas DataFrame
- Create a Scatter Plot from Pandas DataFrame
- Pandas – Replace Values in a DataFrame
- Pandas – fillna with values from another column