How to check Pandas Dataframe is empty

Python Pandas

In this, we are going to learn how to check Pandas Dataframe is empty using some practical examples.We are going to use dataframe property dataframe.empty and dataframe.shape() and built-in len() method.The dataframe is empty which means it does not contain any data.

Ways to check Dataframe is Empty


These are the way to check if the dataframe is empty that we will understand with examples one by one.

  • Dataframe.Empty
  • Dataframe .shape
  • Using len() method
  • Using len(df.index.values) method by passing

Pandas Dataframe empty property


The dataframe. empty property used to check dataframe is empty. It returns a boolean value. When the value is TRUE mean dataframe is empty Else when the boolean value is False means the dataframe is not empty.

if the dataframe contains the ‘NAN’ value then Dataframe. empty property will return False. We will understand this with an example later.

Syntax

dataframe.empty

Return value: It returns true if the dataframe does not contain any data.

1. Dataframe.Empty to check dataframe is empty


In this example, we are using the df. empty property to check whether the dataframe is empty.In below example if condition( df. empty == True) return true that mean dataframe is empty.

Based on the return boolean value we are printing our result in the below example it returns true, So output message is “Dataframe is empty”

Program Example

import pandas as pd



 
df = pd.DataFrame(columns=['Name','Marks','Subject'])

print(df)

if df.empty == True:
 print('Dataframe is empty')
else:
 print('Dataframe is not empty')

Output

Empty DataFrame
Columns: [Name, Marks, Subject]
Index: []
Dataframe is empty

2. Check dataframe is empty with NAN values


The dataframe. empty property returns False if dataframe has NAN value. Let us understand with the below example.

Program Example

import pandas as pd



 
df = pd.DataFrame(columns=['Name','Marks','Subject'] ,index =['R1','R2','R3'])

print(df)
if df.empty == True:
 print('Dataframe is empty')
else:
 print('DataFrame is not empty')

Output

   Name Marks Subject
R1  NaN   NaN     NaN
R2  NaN   NaN     NaN
R3  NaN   NaN     NaN
DataFrame is not empty

3.Dataframe.shape to check Dataframe is empty


The dataframe.shape property returns a tuple of dataframe dimensions (number of rows and columns).If The value of return tuple dimensional has 0 value at 0th index that means the dataframe has zero number of rows and it is empty. So by checking the element value at the 0th index we can find out if the dataframe is empty or not.

The dataframe.shape property returns numbers of rows and columns if the dataframe is not empty

Syntax

dataframe.shape

Return value: returns a tuple of dataframe dimensions (number of rows and columns)

Program Example

import pandas as pd



 
df = pd.DataFrame(columns=['Name','Marks','Subject'])

print(df)
if df.shape[0] == 0:
 print('Dataframe is empty')
else:
 print('DataFrame is not empty')

Output

Empty DataFrame
Columns: [Name, Marks, Subject]
Index: []
Dataframe is empty

4.len() method to check Pandas dataframe is empty


The python built-in len() function is used to check the length of the python object. We will pass the dataframe object to len() function to get the length of the dataframe. If the dataframe is empty it returns 0 else returns a number of rows and columns.

Program Example

import pandas as pd



 
df = pd.DataFrame(columns=['Name','Marks','Subject'])

print(df)
if len(df) == 0:
 print('Dataframe is empty')
else:
 print('DataFrame is not empty')

Output

Empty DataFrame
Columns: [Name, Marks, Subject]
Index: []
Dataframe is empty

5. Check dataframe is empty by index length


The python len() function is used to find the length of the python object. in this example instead of passing the dataframe to the len() function. We will pass the dataframe. index.values, if the length is zero then the dataframe, is empty else not.

Program Example

import pandas as pd



 
df = pd.DataFrame(columns=['Name','Marks','Subject'])

print(df)
if len(df.index.values) == 0:
 print('Dataframe is empty')
else:
 print('DataFrame is not empty')

Output

Empty DataFrame
Columns: [Name, Marks, Subject]
Index: []
Dataframe is empty

Summary

In this post, we have learned multiple ways of How to check Pandas Dataframe is empty using the dataframe. empty,dataframe. shape,and python built-in len() method (bypassing dataframe object, and dataframe.index.values).