• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / Check if pandas DataFrame is Empty in Python

Check if pandas DataFrame is Empty in Python

August 19, 2022 Leave a Comment

There are a few ways you can check if a DataFrame is empty. The easiest way to check if a pandas DataFrame is empty is with the empty property.

import pandas as pd

empty_dataframe = pd.DataFrame()

print(empty_dataframe.empty)

#Output:
True

You can also check if a DataFrame is empty by checking if the length is 0 or the length of the index is 0.

import pandas as pd

empty_dataframe = pd.DataFrame()

print(len(empty_dataframe) == 0)
print(len(empty_dataframe.index) == 0)

#Output:
True
True

When working with the pandas module in Python, the ability to perform different checks on our DataFrames easily is valuable.

One such situation is if you want to check if a DataFrame is empty or not.

There are a few ways you can check if a DataFrame is empty. The easiest way to check if a pandas DataFrame is empty is with the empty property.

Below shows you how to check if a DataFrame is empty with the empty property.

import pandas as pd

empty_dataframe = pd.DataFrame()

print(empty_dataframe.empty)

#Output:
True
import pandas as pd

empty_dataframe = pd.DataFrame()

print(empty_dataframe.empty)

#Output:
True

How to Check if pandas DataFrame is Empty with len()

Another way you can check if a DataFrame is empty is with len().

Empty DataFrames do not have an index, have no columns and have no rows.

You can see this when you print an empty DataFrame to the console.

import pandas as pd

empty_dataframe = pd.DataFrame()

print(empty_dataframe)

#Output:
Empty DataFrame
Columns: []
Index: []

Since there is no index or columns in an empty DataFrame, the length of an empty DataFrame is 0.

Therefore, you can also check if a DataFrame is empty by checking if the length is 0 or the length of the index is 0 as shown below.

import pandas as pd

empty_dataframe = pd.DataFrame()

print(len(empty_dataframe) == 0)
print(len(empty_dataframe.index) == 0)

#Output:
True
True

Hopefully this article has been useful for you to learn how to check if a DataFrame is empty in Python.

Other Articles You'll Also Like:

  • 1.  Solve Python Object Does Not Support Indexing Error
  • 2.  Find Index of Maximum in List Using Python
  • 3.  Using Python to Count Number of True in List
  • 4.  Python Check if Dictionary Value is Empty
  • 5.  Change Python Turtle Background Color with screensize() Function
  • 6.  Using Python to Convert Integer to String with Leading Zeros
  • 7.  Draw Rectangle in Python Using turtle Module
  • 8.  List of Turtle Shapes in Python
  • 9.  Calculate Compound Interest in Python
  • 10.  Python Random Uniform – Generate Random Numbers Uniformly in Range

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

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

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy