How to Check if Pandas DataFrame is Empty?

Pandas – Check if DataFrame is Empty

You can check if a Pandas DataFrame is empty or not. To check if DataFrame is empty in Pandas, use DataFrame.empty.

DataFrame.empty returns a boolean indicator if the DataFrame is empty or not. If the DataFrame is empty, True is returned. If the DataFrame is not empty, False is returned.

Python Pandas - Check if DataFrame is empty

Example 1: Empty DataFrame

In this example, we will initialize an empty DataFrame and check if the DataFrame is empty using DataFrame.empty attribute.

Python Program

import pandas as pd

#initialize a dataframe
df = pd.DataFrame()

isempty = df.empty
print('Is the DataFrame empty :', isempty)
Run this program ONLINE

df = pd.DataFrame() initializes an empty dataframe. And then df.empty checks if the dataframe is empty. Since the dataframe is empty, we will get boolean value True to the variable isempty.

Output

Is the DataFrame empty : True

Example 2: Non-empty DataFrame

In the following example, we have initialized a DataFrame with some rows and then check if DataFrame.empty returns False.

Python Program

import pandas as pd

#initialize a dataframe
df = pd.DataFrame(
	[[21, 72, 67.1],
	[23, 78, 69.5],
	[32, 74, 56.6],
	[52, 54, 76.2]],
	columns=['a', 'b', 'c'])

isempty = df.empty
print('Is the DataFrame empty :', isempty)
Run this program ONLINE

Output

Is the DataFrame empty : False

Summary

In this Pandas Tutorial, we learned how to check if a Pandas DataFrame is empty or not.