Python Pandas - Check if the index has unique values

PythonPandasServer Side ProgrammingProgramming

To check if the index has unique values, use the index.is_unique.

At first, import the required libraries −

import pandas as pd

Let us create the index −

index = pd.Index([50, 40, 30, 20, 10])

Display the index −

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

Check if the index is having unique values −

print("\nIs the Pandas index having unique values?\n",index.is_unique)

Example

Following is the code −

import pandas as pd

# Creating the index
index = pd.Index([50, 40, 30, 20, 10])

# Display the index
print("Pandas Index...\n",index)

# Return an array representing the data in the Index
print("\nArray...\n",index.values)

# Check if the index is having unique values
print("\nIs the Pandas index having unique values?\n",index.is_unique)

Output

This will produce the following code −

Pandas Index...
Int64Index([50, 40, 30, 20, 10], dtype='int64')

Array...
[50 40 30 20 10]

Is the Pandas index having unique values?
True
raja
Published on 13-Oct-2021 11:11:26
Advertisements