How to check if a pandas dataframe is empty in python ?

Published: December 03, 2022

Tags: Python; Pandas; Dataframe;

DMCA.com Protection Status

Examples of how to check if a pandas DataFrame is empty:

Create an empty dataframe

Let's create an empty dataframe with pandas

import pandas as pd

df = pd.DataFrame()

print(df)

gives

Empty DataFrame
Columns: []
Index: []

Check if dataframe is empty

To check if a dataframe is empty a solution is to use "empty":

df.empty

returns

True

here.

With a if statement:

if df.empty:
    print("Oups, your dataframe is empty !")

Another example

df = pd.DataFrame([1,2,3])

print(df)

gives

   0
0  1
1  2
2  3

and

df.empty

gives then

False