Python | Pandas Index.drop_duplicates()
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:
Last Updated :
16 Sep, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...