There are a few ways you can check if a DataFrame is empty. The easiest way to check if a pandas DataFrame is empty is with the empty property.

import pandas as pd

empty_dataframe = pd.DataFrame()

print(empty_dataframe.empty)

#Output:
True

You can also check if a DataFrame is empty by checking if the length is 0 or the length of the index is 0.

import pandas as pd

empty_dataframe = pd.DataFrame()

print(len(empty_dataframe) == 0)
print(len(empty_dataframe.index) == 0)

#Output:
True
True

When working with the pandas module in Python, the ability to perform different checks on our DataFrames easily is valuable.

One such situation is if you want to check if a DataFrame is empty or not.

There are a few ways you can check if a DataFrame is empty. The easiest way to check if a pandas DataFrame is empty is with the empty property.

Below shows you how to check if a DataFrame is empty with the empty property.

import pandas as pd

empty_dataframe = pd.DataFrame()

print(empty_dataframe.empty)

#Output:
True
import pandas as pd

empty_dataframe = pd.DataFrame()

print(empty_dataframe.empty)

#Output:
True

How to Check if pandas DataFrame is Empty with len()

Another way you can check if a DataFrame is empty is with len().

Empty DataFrames do not have an index, have no columns and have no rows.

You can see this when you print an empty DataFrame to the console.

import pandas as pd

empty_dataframe = pd.DataFrame()

print(empty_dataframe)

#Output:
Empty DataFrame
Columns: []
Index: []

Since there is no index or columns in an empty DataFrame, the length of an empty DataFrame is 0.

Therefore, you can also check if a DataFrame is empty by checking if the length is 0 or the length of the index is 0 as shown below.

import pandas as pd

empty_dataframe = pd.DataFrame()

print(len(empty_dataframe) == 0)
print(len(empty_dataframe.index) == 0)

#Output:
True
True

Hopefully this article has been useful for you to learn how to check if a DataFrame is empty in Python.

Categorized in:

Python,

Last Update: March 11, 2024