
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Python Pandas - Check if the index has duplicate values
To check if the index has duplicate values, use the index.has_duplicates property in Pandas.
At first, import the required libraries −
import pandas as pd
Creating the index −
index = pd.Index(['Car','Bike','Truck','Car','Airplane'])
Display the index −
print("Pandas Index...\n",index)
Check if the index is having duplicate values −
print("\nIs the Pandas index having duplicate values?\n",index.has_duplicates)
Example
Following is the code −
import pandas as pd # Creating the index index = pd.Index(['Car','Bike','Truck','Car','Airplane']) # 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 duplicate values print("\nIs the Pandas index having duplicate values?\n",index.has_duplicates)
Output
This will produce the following code −
Pandas Index... Index(['Car', 'Bike', 'Truck', 'Car', 'Airplane'], dtype='object') Array... ['Car' 'Bike' 'Truck' 'Car' 'Airplane'] Is the Pandas index having duplicate values? True
- Related Articles
- Python Pandas - Check if the index has unique values
- Python Pandas - Check if the index has NaNs
- Python Pandas - Indicate duplicate index values
- Python Pandas - Return Index with duplicate values removed
- Python Pandas - Return Index with duplicate values completely removed
- Python Pandas - Indicate all duplicate index values as True
- Python Pandas - Indicate duplicate index values except for the first occurrence
- Python Pandas - Indicate duplicate index values except for the last occurrence
- Python Pandas - Check if the Pandas Index holds Interval objects
- Python Pandas - Return Index with duplicate values removed except the first occurrence
- Python Pandas - Return Index with duplicate values removed keeping the last occurrence
- Python - Check if the Pandas Index holds categorical data
- Python - Check if the Pandas Index only consists of booleans
- Python - Check if the Pandas Index is a floating type
- Python - Check if the Pandas Index only consists of integers

Advertisements