If you want to utilize this function, the following syntax needs to be followed:
Syntax:
Parameter:
The “Keep” parameter is used to regulate how to handle the duplicate values. “Keep” is needed. By default, the value is “first”.
- When the value is “first”, the program treats the first item as distinct and the other identical values as duplicates. This, with the exception of the first instance, eliminates the duplicates.
- If the value is set to “last”, it treats the last entry as unique and the other identical values as duplicates. It then eliminates all duplicates except the last occurrence of that value.
- If the “keep” parameter has the “False” value, all identical values are treated as duplicates. It drops all of the duplicate values from the list.
Example 1: Without Parameters
In this example, we have an index named “index1” that holds 10 integers. Let’s remove the duplicates without passing any parameter to the drop_duplicates() function.
# Create pandas Index that hold 10 values
index1 = pandas.Index([45,67,45,89,45,89,12,34,67,89])
print("Actual Index: ",index1)
print("Unique Index: ",index1.drop_duplicates())
Output:
Explanation:
Unique indices are returned by removing the duplicates.
Example 2: With Keep as False
Let’s have an index that holds 5 strings with duplicates. Now, set the “keep” parameter to False.
# Create pandas Index that hold 5 strings
index1 = pandas.Index(['i1','i1','i4','i5','i4'])
print("Actual Index: ",index1)
print("Unique Index: ",index1.drop_duplicates(keep=False))
Output:
Explanation:
There is only one unique index – “i5”. It is returned by removing all the duplicates.
Example 3: With Keep as First
Let’s have the “index1” with 10 values and “index2” with 5 strings. Set “keep” to “first” to drop the duplicates without removing the first occurrence.
# Create pandas Index that hold 10 values
index1 = pandas.Index([45,67,45,89,45,89,12,34,67,89])
print("Actual Index 1: ",index1)
# Drop duplicates without removing the first occurrence
print("Unique Index 1: ",index1.drop_duplicates(keep ='first'))
# Create pandas Index that hold 5 strings
index2 = pandas.Index(['i1','i1','i4','i5','i4'])
print("Actual Index 2: ",index2)
# Drop duplicates without removing the first occurrence
print("Unique Index 2: ",index2.drop_duplicates(keep='first'))
Output:
Explanation:
- In “index1”, [45, 67, 89, 12, 34] are the first occurrence of unique values.
- In “index2”, [‘i1’, ‘i4’, ‘i5’] are the first occurrence of unique values.
Example 4: With Keep as Last
Let’s have the “index1” with 10 values and “index2” with 5 strings. Set “keep” to “first” to drop the duplicates without removing the first occurrence.
# Create pandas Index that hold 10 values
index1 = pandas.Index([45,67,45,89,45,89,12,34,67,89])
print("Actual Index 1: ",index1)
# Drop duplicates without removing the last occurrence
print("Unique Index 1: ",index1.drop_duplicates(keep ='last'))
# Create pandas Index that hold 5 strings
index2 = pandas.Index(['i1','i1','i4','i5','i4'])
print("Actual Index 2: ",index2)
# Drop duplicates without removing the last occurrence
print("Unique Index 2: ",index2.drop_duplicates(keep='last'))
Output:
Explanation:
- In “index1”, [45, 12, 34, 67, 89] are the last occurrence of unique values.
- In “index2”, [‘i1’, ‘i5’, ‘i4’] are the last occurrence of unique values.
Conclusion
This tutorial is based on the concept of dropping the duplicate indexes using the Pandas module. We utilized the Pandas “Index.drop_duplicates()” method. We provided the syntax for the utilization of this method and also described its parameters. This method gives us three choices for dealing with duplicate values. Every step in this article is explained very clearly and simply.