Test whether pandas DataFrame is Empty in Python (Example Code)

In this Python programming post you’ll learn how to test whether a pandas DataFrame is empty.

Setting up the Example

import pandas as pd                                # Import pandas library to Python
my_df = pd.DataFrame({'A':[5, 1, 9, 5, 1, 2, 8],  # Construct example DataFrame in Python
                      'B':[5, 6, 1, 8, 1, 9, 4],
                      'C':['a', 'b', 'c', 'd', 'e', 'f', 'g']})
print(my_df)                                      # Display example DataFrame in console
#    A  B  C
# 0  5  5  a
# 1  1  6  b
# 2  9  1  c
# 3  5  8  d
# 4  1  1  e
# 5  2  9  f
# 6  8  4  g

Example: Check if pandas DataFrame is Empty Using empty Attribute

print(my_df.empty)                                # DataFrame is not empty
# False

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed

Menu
Top