Python | Pandas Index.drop_duplicates()
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
# importing pandas as pd import pandas as pd # Creating the Index idx = pd.Index([ 10 , 11 , 5 , 5 , 22 , 5 , 3 , 11 ]) # drop all duplicate occurrences of the # labels and keep the first occurrence 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
# importing pandas as pd import pandas as pd # Creating the Index idx = pd.Index([ 10 , 11 , 5 , 5 , 22 , 5 , 3 , 11 ]) # drop all duplicate occurrences of the labels idx.drop_duplicates(keep = False ) # Print the Index idx |
Output:
Contact Us