
- 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 - Indicate duplicate index values
To indicate duplicate index values, use the index.duplicated() method.
At first, import the required libraries −
import pandas as pd
Creating the indexwith some duplicates −
index = pd.Index(['Car','Bike','Airplane','Ship','Airplane'])
Display the index −
print("Pandas Index with duplicates...\n",index)
Indicate duplicate index values as True, rest False. By default, it keeps the first occurrence of the duplicate value unmarked −
print("\nIndicating duplicate values...\n", index.duplicated())
Example
Following is the code −
import pandas as pd # Creating the index with some duplicates index = pd.Index(['Car','Bike','Airplane','Ship','Airplane']) # Display the index print("Pandas Index with duplicates...\n",index) # Return the dtype of the data print("\nThe dtype object...\n",index.dtype) # get the dimensions of the data print("\nGet the dimensions...\n",index.ndim) # Indicate duplicate index values as True, rest False # By default it keeps the first occurrence of the duplicate value unmarked print("\nIndicating duplicate values...\n", index.duplicated())
Output
This will produce the following code −
Pandas Index with duplicates... Index(['Car', 'Bike', 'Airplane', 'Ship', 'Airplane'], dtype='object') The dtype object... object Get the dimensions... 1 Indicating duplicate values... [False False False False True]
- Related Articles
- 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 - Return Index with duplicate values removed
- Python Pandas - Check if the index has duplicate values
- Python Pandas - Return Index with duplicate values completely removed
- 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 - Remove duplicate values from a Pandas DataFrame
- Python Pandas - Return Index without NaN values
- Python Pandas - Return unique values in the index
- Python Pandas - Return a list of the Index values
- Python Pandas - Check if the index has unique values
- Select DataFrame rows between two index values in Python Pandas
- Python Pandas - Replace index values where the condition is False

Advertisements