Explain

How do I check if a pandas DataFrame is empty?

In Pandas, you can directly use the df.empty property to check if a DataFrame has no rows (and no columns):

import pandas as pd

df = pd.DataFrame()

if df.empty:
    print("DataFrame is empty!")
else:
    print("DataFrame is not empty.")
  • df.empty returns True if the DataFrame contains no entries (i.e., len(df.index) == 0), otherwise False.

Alternatively:

  • df.shape[0] == 0 checks if the row count is zero.
  • df.size == 0 checks if the total number of elements (rows × columns) is zero.

However, df.empty is the most direct and readable option for confirming emptiness.

Recommended Resources

  1. Grokking Python Fundamentals

Recommended Courses