Python | Pandas Index.unique()



Python | Pandas Index.unique()

Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier.

Pandas Index.unique() function return unique values in the index. Uniques are returned in order of appearance, this does NOT sort.

Syntax: Index.unique(level=None)

Parameters :
level : Only return values from specified level (for MultiIndex)

Returns : Index without duplicates

Example #1: Use Index.unique() function to return unique values in the index.

# importing pandas as pd
import pandas as pd
 
# Creating the index
idx = pd.Index(['Harry', 'Mike', 'Arther', 'Nick',
                'Harry', 'Arther'], name ='Student')
 
# Print the Index
print(idx)

Output :

Let’s find all the unique values in the Index.

# find unique values in the index
idx.unique()

Output :

The function has returned a new index having all the unique values of the original index.

Example #2: Use Index.unique() function to find the unique values in the index

# importing pandas as pd
import pandas as pd
 
# Creating the index
idx = pd.Index([21, 10, 30, 40, 50, 10, 50])
 
# Print the Index
print(idx)

Output :

Let’s find all the unique values in the index

# for finding the unique values in the index
idx.unique()

Output :

The function has returned a new index that contains all of the unique values in the original index.

 

Last Updated on October 19, 2021 by admin

Leave a Reply

Your email address will not be published. Required fields are marked *

Recommended Blogs