Python Pandas - Indicate all duplicate index values as True

PythonPandasServer Side ProgrammingProgramming

To indicate all duplicate index values as True, use the index.duplicated(). Use the keep parameter with the value False.

At first, import the required libraries −

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)

Indicate all duplicate index values as True. Set the "keep" parameter as "False" −

print("\nIndicating all duplicate index values True...\n", index.duplicated(keep=False))

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 all duplicate index values as True
# Set the "keep" parameter as "False"
print("\nIndicating all duplicate index values True...\n", index.duplicated(keep=False))

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 all duplicate index values True...
[False False True False True]
raja
Updated on 13-Oct-2021 11:41:27

Advertisements