How to check if a dataframe is empty in Python [4 Methods]

Do you want to check if a dataframe is empty or not? In this Pandas tutorial, I will explain how to check if a dataframe is empty in Python using different methods with some illustrative examples.

To add rows to a Pandas DataFrame in a loop in Python, we can utilize attributes like empty to check if the DataFrame is empty, shape to get the row count, len() function for the length of the DataFrame, and size for the total number of elements.

Check if a dataframe is empty in Python

There are four different methods to check if a dataframe is empty in Python:

  1. The empty Attribute
  2. Checking the Shape of the DataFrame
  3. The len() Function
  4. The size Attribute

Let’s see them one by one using some illustrative examples:

1. Check if a dataframe is empty using the empty attribute

This method involves using the empty attribute of the DataFrame to check if a dataframe is empty in Python. It returns True if the DataFrame is empty (having no rows and no columns) and False otherwise.

import pandas as pd

national_parks_df = pd.DataFrame()
if national_parks_df.empty:
    print("No data on U.S. national parks available.")

Output:

No data on U.S. national parks available.

Below is a screenshot showcasing the output after executing the code in the Pycharm editor.

How to check if a dataframe is empty in Python

2. Pandas check if df is empty using the shape attribute

By examining the shape attribute of a DataFrame, which returns a tuple (number of rows, number of columns), we can determine if it’s empty. If either the number of rows or columns is zero, the DataFrame is considered empty.

Here is the code to check if a dataframe is empty in Python using the shape attribute:

import pandas as pd

states_df = pd.DataFrame(columns=['State', 'Population'])
if states_df.shape[0] == 0:
    print("No state information is available.")

Output:

No state information is available.

After executing the code in Pycharm, one can see the output in the below screenshot.

how to check if dataframe is empty in Python

3. Check if df empty using the len() function

Applying the len() function to a DataFrame returns the number of rows. If len(df) returns 0, it indicates that the DataFrame is empty.

Here is the code, to check if a dataframe is empty in Python using the len() function:

import pandas as pd

covid_cases_df = pd.DataFrame()
if len(covid_cases_df) == 0:
    print("No COVID-19 data for U.S. cities is available today.")

Output:

No COVID-19 data for U.S. cities is available today.

A screenshot is mentioned below, after implementing the code in the Pycharm editor.

pandas check if dataframe is empty in Python

4. Check if dataframe is empty using the size attribute

The size attribute of a DataFrame returns the total number of elements in the DataFrame, which is the product of the number of rows and columns. If the DataFrame is empty, the size will return 0.

This method is particularly useful when we want to confirm that the DataFrame contains no data at all (neither rows nor columns).

import pandas as pd

cities_df = pd.DataFrame(columns=['City', 'Population'])
if cities_df.size == 0:
    print("No data available on American cities and their populations.")
else:
    print("Data is available for analysis.")

Output:

No data available on American cities and their populations.

After implementing the code in the Pycharm editor, the screenshot is mentioned below.

python check if dataframe is empty

Conclusion

Here, I have explained how to add rows to a Pandas DataFrame in a loop in Python, showcasing four methods: using the empty attribute to check DataFrame emptiness, the shape attribute to determine the number of rows, the len() function to get the DataFrame’s length, and the size attribute to assess overall data volume.

These examples illustrate various techniques to effectively and dynamically append data to a DataFrame within loop constructs.

You may also like to read: