How to check if dataframe is empty – Pandas : 4 Ways to check if a DataFrame is empty in Python

How to check if a dataframe is empty in Python ?

Python check if dataframe is empty: In this article we will see different ways to check if a dataframe is empty in python.

Approach-1 : Check whether dataframe is empty using Dataframe.empty :

How to check if dataframe is empty: There is an empty attribute provided by dataframe class in python.

Syntax - Dataframe.empty

If it returns True then the dataframe is empty.

# Program :

import pandas as pd

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

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

Even if it contains NaN then also it returns the data frame is empty.

# Program :

import pandas as pd
import numpy as np

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

# Dataframe object created
dfObj = pd.DataFrame(columns=['Your Name', 'Your Age', 'Your City'])

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

Approach-2 : Check if dataframe is empty using Dataframe.shape :

There is an shape attribute provided by dataframe class in python.

Syntax- Dataframe.shape

shape attribute return a tuple containing dimension of dataframe. Like if in the dataframe there is 3 rows and 4 columns then it will return (3,4). If the dataframe is empty then it will return 0 at 0th index.

# 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

Approach-3 : Check if dataframe is empty by checking length of index :

Dataframe.index represents indices of Dataframe. If the dataframe is empty then size will be 0.

# Program :

import pandas as pd
import numpy as np

# empty Dataframe object created
dfObj = pd.DataFrame(columns=['Date', 'UserName', 'Action'])
# checking if length of index is 0 or not
if len(dfObj.index.values) == 0:
    print('DataFrame is empty')
else:
    print('DataFrame is not empty')
Output :
DataFrame is empty

Approach-4 : Check if dataframe is empty by using len on Datafarme :

Directly by calling the len() function we can also check the dataframe is empty or not. If the length of dataframe is 0 then it the dataframe is empty.

# Program :

import pandas as pd
import numpy as np

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

# checking if length of dataframe is 0 or not
# by calling len()
if len(dfObj) == 0:
    print('DataFrame is empty')
else:
    print('DataFrame is not empty')
Output :
DataFrame is not empty

Want to expert in the python programming language? Exploring Python Data Analysis using Pandas tutorial changes your knowledge from basic to advance level in python concepts.

Read more Articles on Python Data Analysis Using Padas – Find Elements in a Dataframe