4 Ways to Check if a DataFrame is Empty

Pandas Check If A DataFrame Is Empty

Welcome to this comprehensive guide on how to determine if a DataFrame is empty using Python’s popular library, Pandas. In this data-driven world, managing and analyzing data effectively is an important skillset.

Pandas is a powerful tool for data manipulation, and its DataFrame object is widely used for handling tabular data structures. In this tutorial, we’ll learn how to check if a DataFrame is empty with four different methods. By the end of this tutorial, you’ll be able to handle empty DataFrames and make your data analysis process more efficient.

4 Ways to Check if a DataFrame is Empty

So, let’s dive into these techniques and boost your Python and Pandas skills!


Method 1: Check DataFrame Emptiness with DataFrame.empty Attribute

This is the most commonly used method to check if a Pandas DataFrame object is empty or not. In this method, we will be using the DataFrame.empty attribute of the Pandas DataFrame class.

When the DataFrame.empty attribute is applied on a pandas DataFrame object, it returns a boolean value i.e True or False. First, it checks if the DataFrame object is empty it returns True and if the DataFrame object is not empty it returns False. Let’s implement this through Python code.

# Import pandas module
import pandas as pd 

# Create an empty DataFrame
# Using pd.DataFrame() function
df1 = pd.DataFrame()
print('\nThis is DataFrame-1:')
print(df1)

# Create a non-empty DataFrame
# Using pd.DataFrame() function
df2 = pd.DataFrame({'Char': ['A', 'B', 'C', 'D', 'E'],
                    'ASCII': [65, 66, 67, 68, 69]})
print('\nThis is DataFrame-2: ')
print(df2)

# Check if the above created DataFrames are empty 
# Or not using DataFrame.empty attribute
print(f'\nDataFrame-1 is empty: {df1.empty}')
print(f'\nDataFrame-2 is empty: {df2.empty}')

Output:

This is DataFrame-1:
Empty DataFrame
Columns: []
Index: []

This is DataFrame-2:
  Char  ASCII
0    A     65
1    B     66
2    C     67
3    D     68
4    E     69

DataFrame-1 is empty: True

DataFrame-2 is empty: False

Method 2: Determine DataFrame Emptiness Using DataFrame.shape Attribute

This is the second most commonly used method to check if the given Pandas DataFrame is empty or not. In this method, we will be using the DataFrame.shape attribute of the Pandas DataFrame class.

The shape attribute returns a tuple representing the dimensions (i.e. number of rows and columns) of the DataFrame object. In order to check if the DataFrame object is empty or not, we have to apply the shape attribute on the DataFrame object.

Then it checks if the DataFrame object is empty. It returns zero value for the zeroth index of the returned tuple object representing that the DataFrame has zero number of rows in it.

And if the DataFrame object is not empty, it returns the number of rows in the DataFrame object. Let’s write Python code to implement this.

# Import pandas module
import pandas as pd 

# Create an empty DataFrame with 5 columns
# Using pd.DataFrame() function
df1 = pd.DataFrame(columns = ['A', 'B', 'C', 'D', 'E'])
print('\nThis is DataFrame-1:')
print(df1)

# Create a non-empty DataFrame with 5 rows & 2 columns
# Using pd.DataFrame() function
df2 = pd.DataFrame({'Char': ['A', 'B', 'C', 'D', 'E'],
                    'ASCII': [65, 66, 67, 68, 69]})
print('\nThis is DataFrame-2:')
print(df2)

# Check if the above created DataFrames are empty 
# Or not using DataFrame.shape attribute
print(f'\nNumber of rows in DataFrame-1: {df1.shape[0]}')
print(f'\nNumber of rows in DataFrame-2: {df2.shape[0]}')

Output:

This is DataFrame-1:
Empty DataFrame
Columns: [A, B, C, D, E]
Index: []

This is DataFrame-2:
  Char  ASCII
0    A     65
1    B     66
2    C     67
3    D     68
4    E     69

Number of rows in DataFrame-1: 0

Number of rows in DataFrame-2: 5

Method 3: Verify DataFrame Emptiness by Passing DataFrame to len() Function

This is one of the less commonly used methods to check if the given pandas DataFrame object is empty or not. In this method we will be using the len() function. To check if the DataFrame is empty or not, we can directly pass the pandas DataFrame object to the len() function.

If the passed DataFrame object is an empty DataFrame then the len() function returns a zero value representing that the DataFrame object has zero number of rows in it. But if the passed DataFrame object is not empty, then the len() function returns a non-zero value representing the number of rows in the DataFrame object. Let’s implement this through Python code.

# Import pandas module
import pandas as pd 

# Create an empty DataFrame with 3 columns
# Using pd.DataFrame() function
df1 = pd.DataFrame(columns = ['C1', 'C2', 'C3'])
print('\nThis is DataFrame-1:')
print(df1)

# Create a non-empty DataFrame with 4 rows & 2 columns
# Using pd.DataFrame() function
df2 = pd.DataFrame({'Char': ['a', 'b', 'c', 'd'], 'ASCII': [97, 98, 99, 100]})
print('\nThis is DataFrame-2:')
print(df2)

# Check if the above created DataFrames are empty 
# Or not passing the DataFrame object to the len() function
print(f'\nLength of DataFrame-1: {len(df1)}')
print(f'\nLength of DataFrame-2: {len(df2)}')

Output:

This is DataFrame-1:
Empty DataFrame
Columns: [C1, C2, C3]
Index: []

This is DataFrame-2:
  Char  ASCII
0    a     97
1    b     98
2    c     99
3    d    100

Length of DataFrame-1: 0

Length of DataFrame-2: 4

In the above output, the length of the DataFrame represents the number of rows in it. That’s why the length of the empty DataFrame is zero as it has no rows in it while the length of the non-empty DataFrame is non-zero i.e. it is equal to the number of rows in it.

Method 4: Confirm DataFrame Emptiness by Checking DataFrame Index Length

This is one of the less common methods to check if a given Pandas DataFrame object is empty or not. Here also we will be using the len() function to check if the DataFrame is empty or not. But instead of passing the whole pandas DataFrame object to the len() function, we can pass the DataFrame index list to the len() function.

We can obtain the DataFrame index list using the DataFrame.index.values attribute of the pandas DataFrame class which returns a Python list containing the index of the DataFrame object as its elements.

If the passed DataFrame index list is empty, then the len() function returns a zero value. That means the DataFrame has zero number of rows. But if the passed DataFrame index list is not empty, then the len() function returns a non-zero value that means the DataFrame index list has some values. Let’s see the Python code to implement this.

# Import pandas module
import pandas as pd 

# Create an empty DataFrame with 3 columns
# Using pd.DataFrame() function
df1 = pd.DataFrame(columns = ['Col-1', 'Col-2', 'Col-3'])
print('\nThis is DataFrame-1:')
print(df1)

# Create a non-empty DataFrame with 3 rows & 2 columns
# Using pd.DataFrame() function
df2 = pd.DataFrame({'Col-1': ['Python', 'Matlab', 'Csharp'],
                    'Col-2': ['.py', '.mat', '.cs']}, index = ['i', 'ii', 'iii'])
print('\nThis is DataFrame-2:')
print(df2)

# Obtain the DataFrame index list for
# DataFrame-1 & DataFrame-2
# Using the DataFrame.index.values attribute
print(f'\nIndex list of DataFrame-1: {df1.index.values}')
print(f'\nIndex list of DataFrame-2: {df2.index.values}')

# Check if the above created DataFrames are empty 
# Or not passing the DataFrame index list to the len() function
print(f'\nLength of DataFrame-1 index list: {len(df1.index.values)}')
print(f'\nLength of DataFrame-2 index list: {len(df2.index.values)}')

Output:

This is DataFrame-1:
Empty DataFrame
Columns: [Col-1, Col-2, Col-3]
Index: []

This is DataFrame-2:
      Col-1 Col-2
i    Python   .py
ii   Matlab  .mat
iii  Csharp   .cs

Index list of DataFrame-1: []

Index list of DataFrame-2: ['i' 'ii' 'iii']

Length of DataFrame-1 index list: 0

Length of DataFrame-2 index list: 3

Summing-up

In this tutorial, we have explored four different methods to check if a pandas DataFrame is empty or not. Each method has its own unique approach, but they all serve the same purpose of determining emptiness in a DataFrame. As you continue working with pandas, consider which method works best for your specific use case and coding style.

Now that you know how to check DataFrame emptiness, what other aspects of DataFrame manipulation would you like to learn more about?