Pandas Index.drop_duplicates() function return Index with duplicate values removed in Python.
Syntax of Pandas Index.drop_duplicates()
Syntax: Index.drop_duplicates(labels, errors=’raise’)
Parameters : keep : {‘first’, ‘last’, False}
- ‘first’ : Drop duplicates except for the first occurrence.(default)
- ‘last’ : Drop duplicates except for the last occurrence.
- False : Drop all duplicates.
Returns : deduplicated: Index
Examples of Index.drop_duplicates()
The function provides the flexibility to choose which duplicate value to be retained. We can drop all duplicate values from the list or leave the first/last occurrence of the duplicated values.
Example 1: Use Index.drop_duplicates() function to drop all the occurrences of the duplicate value. Let’s drop all occurrences of duplicate values in the Index except the first occurrence.
Python3
import pandas as pd
idx = pd.Index([ 10 , 11 , 5 , 5 , 22 , 5 , 3 , 11 ])
idx.drop_duplicates(keep = 'first' )
print (idx)
|
Output:
Example 2: Use Index.drop_duplicate() function to drop all duplicate occurrences of the label. Do not keep any duplicated values in the Index.
Python3
import pandas as pd
idx = pd.Index([ 10 , 11 , 5 , 5 , 22 , 5 , 3 , 11 ])
idx.drop_duplicates(keep = False )
idx
|
Output:
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
16 Sep, 2022
Like Article
Save Article