Get unique values of a column in python pandas

In this section we will learn how to get unique values of a column in python pandas using unique() function .  Lets see with an example

Create Dataframe:


import pandas as pd
import numpy as np

#Create a DataFrame
d = {
    'Name':['Alisa','Bobby','jodha','jack','raghu','Cathrine',
            'Alisa','Bobby','kumar','Alisa','Alex','Cathrine'],
    'Age':[26,24,23,22,23,24,26,24,22,23,24,24],
     
       'Score':[85,63,55,74,31,77,85,63,42,62,89,77]}

df = pd.DataFrame(d,columns=['Name','Age','Score'])
df

so the output will be

Get list of unique values in a column in python pandas

 

Get the unique values of a column:

Lets get the unique values of “Name” column


df.Name.unique()

The unique() function gets the list of unique column values . So the output will be

array([‘Alisa’, ‘Bobby’, ‘jodha’, ‘jack’, ‘raghu’, ‘Cathrine’, ‘kumar’,

‘Alex’], dtype=object)

 

 

Get the unique values of “Age” column


df.Age.unique()

so the output will be

array([26, 24, 23, 22], dtype=int64)

 

previous small Get unique values of a column in python pandas                                                                                                           next_small Get list of unique values of a column in python pandas

Author

  • Sridhar Venkatachalam

    With close to 10 years on Experience in data science and machine learning Have extensively worked on programming languages like R, Python (Pandas), SAS, Pyspark.

    View all posts