Checking if a pandas dataframe is empty or not

Pandas – Check if a DataFrame is Empty

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 –

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.

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: []

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:

📚 Data Science Programs By Skill Level

Introductory

Intermediate ⭐⭐⭐

Advanced ⭐⭐⭐⭐⭐

🔎 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.

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.

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 or None 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 the len() function to get the number of rows in a 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 –

Author

  • Piyush Raj

    Piyush is a data professional passionate about using data to understand things better and make informed decisions. He has experience working as a Data Scientist in the consulting domain and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.

Scroll to Top