To return Index with duplicate values removed, use the index.drop_duplicates() method.
At first, import the required libraries −
import pandas as pd
Creating the index with some duplicates −
index = pd.Index(['Car','Bike','Truck','Car','Airplane'])
Display the index −
print("Pandas Index with duplicates...\n",index)
Return Index with duplicate values removed. By default it keeps the first occurrence of the duplicate value removed −
print("\nIndex with duplicate values removed...\n",index.drop_duplicates())
Example
Following is the code −
import pandas as pd # Creating the index with some duplicates index = pd.Index(['Car','Bike','Truck','Car','Airplane']) # Display the index print("Pandas Index with duplicates...\n",index) # Return the dtype of the data print("\nThe dtype object...\n",index.dtype) # get the bytes in the data print("\nGet the bytes...\n",index.nbytes) # get the dimensions of the data print("\nGet the dimensions...\n",index.ndim) # Return Index with duplicate values removed # By default it keeps the first occurrence of the duplicate value removed print("\nIndex with duplicate values removed...\n",index.drop_duplicates())
Output
This will produce the following code −
Pandas Index with duplicates... Index(['Car', 'Bike', 'Truck', 'Car', 'Airplane'], dtype='object') The dtype object... object Get the bytes... 40 Get the dimensions... 1 Index with duplicate values removed... Index(['Car', 'Bike', 'Truck', 'Airplane'], dtype='object')