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

PythonPandasServer Side ProgrammingProgramming

To check if the index is empty with 0 elements, use the index.empty property in Pandas.

At first, import the required libraries −

import pandas as pd

Creating the index −

index = pd.Index([])

Display the index −

print("Pandas Index...
",index)

Check for empty index −

print("
Is the index empty?
",index.empty)

Example

Following is the code −

import pandas as pd

# Creating the index
index = pd.Index([])

# Display the index
print("Pandas Index...
",index) # Return the number of elements in the Index print("
Number of elements in the index...
",index.size) # check for empty index print("
Is the index empty?
",index.empty)

Output

This will produce the following code −

Pandas Index...
Index([], dtype='object')

Number of elements in the index...
0

Is the index empty?
True
raja
Updated on 13-Oct-2021 11:23:38

Advertisements