Python Pandas - Return Index with duplicate values removed keeping the last occurrence



To return Index with duplicate values removed keeping the last occurrence, use the index.drop_duplicates() method. Use the keep parameter with value last.

At first, import the required libraries βˆ’

import pandas as pd

Creating the index with some duplicatesβˆ’

index = pd.Index(['Car','Bike','Airplane','Ship','Airplane'])

Display the index βˆ’

print("Pandas Index with duplicates...\n",index)

Return Index with duplicate values removed. The "keep" parameter with value "last" keeps the last occurrence for each set of duplicated entries βˆ’

print("\nIndex with duplicate values removed (keeping the last occurrence)...\n",index.drop_duplicates(keep='last'))

Example

Following is the code βˆ’

import pandas as pd

# Creating the index with some duplicates
index = pd.Index(['Car','Bike','Airplane','Ship','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

# The "keep" parameter with value "last" keeps the last occurrence for each set of duplicated entries
print("\nIndex with duplicate values removed (keeping the last occurrence)...\n",index.drop_duplicates(keep='last'))

Output

This will produce the following code βˆ’

Pandas Index with duplicates...
Index(['Car', 'Bike', 'Airplane', 'Ship', 'Airplane'], dtype='object')

The dtype object...
object

Get the bytes...
40

Get the dimensions...
1

Index with duplicate values removed (keeping the last occurrence)...
Index(['Car', 'Bike', 'Ship', 'Airplane'], dtype='object')
Updated on: 2021-10-13T11:34:29+05:30

270 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements