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.

Dataframe.empty

It return True if Dataframe contains no data.

Let’s see an example,
Create an empty Dataframe

# Create an empty Dataframe
dfObj = pd.DataFrame(columns=['Date', 'UserName', 'Action'])

Now let’s check if it’s empty using empty attribute,
# Check if Dataframe is empty using empty attribute
if dfObj.empty == True:
    print('DataFrame is empty')
else:
    print('DataFrame is not empty')

Output:
DataFrame is empty

If dataframe contains NaN only, then still empty attribute will return False i.e.
# List of Tuples
students = [(np.NaN, np.NaN, np.NaN),
            (np.NaN, np.NaN, np.NaN),
            (np.NaN, np.NaN, np.NaN)
           ]

# Create a DataFrame object
studentsDfObj = pd.DataFrame(students, columns=['Name', 'Age', 'City'])

# Check if Dataframe is empty using empty attribute
if studentsDfObj.empty == True:
    print('Student DataFrame is empty')
else:
    print('Student DataFrame is not empty')

Output:
Student DataFrame is not empty

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.

Dataframe.shape

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.
# Create an empty Dataframe
dfObj = pd.DataFrame(columns=['Date', 'UserName', 'Action'])

# Check if Dataframe is empty using dataframe's shape attribute
if dfObj.shape[0] == 0:
    print('DataFrame is empty')
else:
    print('DataFrame is not empty')

Output:
DataFrame is empty

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.

# Create an empty Dataframe
dfObj = pd.DataFrame(columns=['Date', 'UserName', 'Action'])

# check if length of index is 0 to verify if dataframe is empty
if len(dfObj.index.values) == 0:
    print('DataFrame is empty')
else:
    print('DataFrame is not empty')

Output:
DataFrame is empty

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.

# check if length of dataframe is 0 by calling len on Dataframe
if len(dfObj) == 0:
    print('DataFrame is empty')
else:
    print('DataFrame is not empty')

Output:
DataFrame is empty

Complete example is as follows,
import pandas as pd
import numpy as np

def main():
    # Create an empty Dataframe
    dfObj = pd.DataFrame(columns=['Date', 'UserName', 'Action'])

    print("Contents of Original Dataframe", dfObj, sep='\n')

    print('*** Check if Dataframe is Empty using empty property ***')

    # Check if Dataframe is empty using empty attribute
    if dfObj.empty == True:
        print('DataFrame is empty')
    else:
        print('DataFrame is not empty')

    # List of Tuples
    students = [(np.NaN, np.NaN, np.NaN),
                (np.NaN, np.NaN, np.NaN),
                (np.NaN, np.NaN, np.NaN)
               ]

    # Create a DataFrame object
    studentsDfObj = pd.DataFrame(students, columns=['Name', 'Age', 'City'])

    # Check if Dataframe is empty using empty attribute
    if studentsDfObj.empty == True:
        print('Student DataFrame is empty')
    else:
        print('Student DataFrame is not empty')


    print('*** Check if dataframe is empty using Dataframe.shape ***')
    print('Shape of Dataframe : ' , dfObj.shape)

    # Check if Dataframe is empty using dataframe's shape attribute
    if dfObj.shape[0] == 0:
        print('DataFrame is empty')
    else:
        print('DataFrame is not empty')

    print('*** Check if dataframe is empty by checking length of index ***')

    # check if length of index is 0 to verify if dataframe is empty
    if len(dfObj.index.values) == 0:
        print('DataFrame is empty')
    else:
        print('DataFrame is not empty')

    print('*** Check if dataframe is empty by call len on Dataframe ***')

    # check if length of dataframe is 0 by calling len on Dataframe
    if len(dfObj) == 0:
        print('DataFrame is empty')
    else:
        print('DataFrame is not empty')


if __name__ == '__main__':
    main()

Output
Contents of Original Dataframe
Empty DataFrame
Columns: [Date, UserName, Action]
Index: []
*** Check if Dataframe is Empty using empty property ***
DataFrame is empty
Student DataFrame is not empty
*** Check if dataframe is empty using Dataframe.shape ***
Shape of Dataframe :  (0, 3)
DataFrame is empty
*** Check if dataframe is empty by checking length of index ***
DataFrame is empty
*** Check if dataframe is empty by call len on Dataframe ***
DataFrame is empty