How to Get Unique Values in Column of Pandas DataFrame

In this post, we will see how to get Unique Values from a Column in Pandas DataFrame.
[toc]

Sometimes, You might want to get unique Values from a Column in large Pandas DataFrame.

Here is a sample Employee data which we will use.
[table id=28 /]

Using unique() method

You can use Pandas unique() method to get unique Values from a Column in Pandas DataFrame.
Here is an example. We will use unique() method to get unique value from Department column.

import pandas as pd
emp_df = pd.DataFrame({'Name': ['Rohit','John','Appy','Smita','Neha'], 
                        'Age': [26,21,34,47,22], 
                       'Department':['Sales','HR','Sales','Tech','HR']})
print("-------Original Dataframe-------\n",emp_df)

# Get unique values using unique() method
deptUniq = emp_df.Department.unique()
print("-------Unique Department values-------\n",deptUniq)
print(type(deptUniq))

Output:

-------Original Dataframe-------
     Name  Age Department
0  Rohit   26      Sales
1   John   21         HR
2   Appy   34      Sales
3  Smita   47       Tech
4   Neha   22         HR
-------Unique Department values-------
 ['Sales' 'HR' 'Tech']

As you can see, we got unique values for Department column.
If you want the result as list, you can use toList() function to convert numpy.ndarray to list.
Change highlight line with below line:

# Get unique values using unique() method
deptUniqList = emp_df.Department.unique().tolist()
print("-------Unique Department values-------\n",deptUniqList)
print(type(deptUniqList))

Output:

-------Original Dataframe-------
     Name  Age Department
0  Rohit   26      Sales
1   John   21         HR
2   Appy   34      Sales
3  Smita   47       Tech
4   Neha   22         HR
-------Unique Department values-------
 ['Sales', 'HR', 'Tech']

Using drop_duplicates() method

You can also use drop_duplicates() to get unique values from a column in Pandas DataFrame. Although this method does not obvious as compared to unique one.

Here is an example. We will use drop_duplicates() method to get unique value from Department column.

import pandas as pd
emp_df = pd.DataFrame({'Name': ['Rohit','John','Appy','Smita','Neha'], 
                        'Age': [26,21,34,47,22], 
                       'Department':['Sales','HR','Sales','Tech','HR']})
print("-------Original Dataframe-------\n",emp_df)

# Get unique values using unique() method
deptUniq = emp_df.Department.drop_duplicates()
print("-------Unique Department values-------\n",deptUniq)
print(type(deptUniq))

Output:

-------Original Dataframe-------
     Name  Age Department
0  Rohit   26      Sales
1   John   21         HR
2   Appy   34      Sales
3  Smita   47       Tech
4   Neha   22         HR
-------Unique Department values-------
 0    Sales
1       HR
3     Tech
Name: Department, dtype: object

As you can see, we got Series as output by using drop_duplicates() method.

[social_share share_on="facebook,twitter,linkedin,whatsapp"][post_navigator in_same_term="true"]

Leave a Reply

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

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.