How to Check if Pandas DataFrame is Empty?

Pandas – Check if DataFrame is empty

To check if DataFrame is empty in Pandas, use DataFrame.empty property.

DataFrame.empty returns a boolean value indicating whether this 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

Examples

1. Check if DataFrame is empty – Positive Scenario

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

Python Program

import pandas as pd

# Initialize a DataFrame 
df = pd.DataFrame()

# Check if DataFrame is empty
if df.empty:
    print('The DataFrame is empty.')
else:
    print('The DataFrame is not empty.')
Run Code Copy

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

Output

The DataFrame is empty.

2. Check for a 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'])

# Check if DataFrame is empty
if df.empty:
    print('The DataFrame is empty.')
else:
    print('The DataFrame is not empty.')
Run Code Copy

Output

The DataFrame is not empty.

Summary

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

Related Tutorials

Code copied to clipboard successfully 👍