Python Pandas - Check if the index is empty with 0 elements

To check if a Pandas Index is empty with 0 elements, use the index.empty property. This property returns True if the index contains no elements, and False otherwise.

Syntax

index.empty

Creating an Empty Index

First, let's create an empty index and check its properties ?

import pandas as pd

# Creating an empty index
index = pd.Index([])

# Display the index
print("Pandas Index...")
print(index)

# Check the size
print("\nNumber of elements in the index:")
print(index.size)

# Check if empty
print("\nIs the index empty?")
print(index.empty)
Pandas Index...
Index([], dtype='object')

Number of elements in the index:
0

Is the index empty?
True

Comparing Empty vs Non-Empty Index

Let's compare an empty index with a non-empty index to see the difference ?

import pandas as pd

# Create empty and non-empty indexes
empty_index = pd.Index([])
non_empty_index = pd.Index(['A', 'B', 'C'])

print("Empty Index:")
print(f"Size: {empty_index.size}")
print(f"Empty: {empty_index.empty}")

print("\nNon-Empty Index:")
print(f"Size: {non_empty_index.size}")
print(f"Empty: {non_empty_index.empty}")
Empty Index:
Size: 0
Empty: True

Non-Empty Index:
Size: 3
Empty: False

Practical Use Case

The empty property is useful for conditional operations on DataFrames ?

import pandas as pd

# Create DataFrames with different index conditions
df1 = pd.DataFrame(index=pd.Index([]))
df2 = pd.DataFrame({'A': [1, 2, 3]}, index=['X', 'Y', 'Z'])

def process_dataframe(df, name):
    if df.index.empty:
        print(f"{name}: Index is empty, cannot process data")
    else:
        print(f"{name}: Index has {df.index.size} elements")
        print(f"Index values: {list(df.index)}")

process_dataframe(df1, "DataFrame 1")
process_dataframe(df2, "DataFrame 2")
DataFrame 1: Index is empty, cannot process data
DataFrame 2: Index has 3 elements
Index values: ['X', 'Y', 'Z']

Conclusion

Use the index.empty property to check if a Pandas Index contains zero elements. This is particularly useful for conditional logic and data validation in your data processing workflows.

Updated on: 2026-03-26T16:15:06+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements