In this article we will discuss four different ways to check if a given dataframe is empty or not.


Check whether dataframe is empty using Dataframe.empty

In Python’s pandas, the Dataframe class provides an attribute empty i.e.

It return True if Dataframe contains no data.

Let’s see an example,
Create an empty Dataframe

Now let’s check if it’s empty using empty attribute,

Output:

If dataframe contains NaN only, then still empty attribute will return False i.e.

Output:

Dataframe contains only NaN but still it contains some data therefore it’s not empty as per empty attribute.

Check if dataframe is empty using Dataframe.shape

Dataframe class provides an attribute shape i.e.

It returns a tuple containing the dimensions of Dataframe.
Like in case our dataframe has 3 rows and 4 columns it will return (3,4). If our dataframe is empty it will return 0 at 0th index i.e.
the count of rows. So, we can check if dataframe is empty by checking if value at 0th index is 0 in this tuple.

Output:

Check if dataframe is empty by checking length of index

As Dataframe.index represents the indices of Dataframe, if dataframe is empty then it’s size will be 0 i.e.

Output:

Check if dataframe is empty by using len on Datafarme

Last but not the least, we can directly call len() on the dataframe to check if dataframe is empty i.e.

Output:

Complete example is as follows,

Output

 

If you didn't find what you were looking, then do suggest us in the comments below. We will be more than happy to add that.

Do Subscribe with us for more Articles / Tutorials like this,