Python | Pandas Index.duplicated ()



Index.duplicated() Pandas Index.duplicated() Specifies duplicate index values. Duplicate values ​​are denoted as True values ​​in the resulting array. All duplicates can be specified, all but the first or all but the last occurrence of duplicates.

Syntax: Index.duplicated (keep = `first`)

Parameters:
keep: {`first`, `last`, False}, default `first`
The value or values ​​in a set of duplicates to mark as missing.
– & gt;  `first`: Mark duplicates as True except for the first occurrence.
– & gt;  `last`: Mark duplicates as True except for the last occurrence.
– & gt;  False: Mark all duplicates as True.

Returns: numpy.ndarray

Example # 1: Use Index.duplicated () to specify all duplicate values ​​in the index except the first.

# import pandas as pd

import pandas as pd

 
# Create index

idx = pd.Index ([ `Labrador` , `Beagle` , ` Labrador`

`Lhasa` , ` Husky` , `Beagle` ])

 
# Print index
idx

Output:

Let`s find out if whether the value present in the Index is duplicate or unique.

# Define duplicate values ​​other than the first

idx.duplicated (keep = `first` )

Output:

As we can see in the output, Index.duplicated () all occurrences of the duplicate value are True except for the first occurrence.

Example # 2: Use Index.duplicated () to identify all duplicate values. here all duplicate values ​​will be marked as True

# import pandas as pd

import pandas as pd

 
# Create index

idx = pd.Index ([ 100 , 50 , 45 , 100 , 12 , 50 , None ])

 
# Print index
idx

Output:

Let`s define all duplicate values ​​in the index.

Note: we have NaN values ​​in the index.

Output:

The function marked all duplicate values ​​as True. He also considered a single occurrence of the NaN value as unique and flagged it as false.


# Determine all duplicate value occurrences

idx.duplicated (keep = False )