In this post, we will see how to get Unique Values from a Column in Pandas DataFrame.
Table of Contents
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.
Name | Age | Department |
---|---|---|
Rohit | 26 | Sales |
John | 21 | HR |
Appy | 34 | Sales |
Smita | 47 | Tech |
Neha | 22 | HR |
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.
1 2 3 4 5 6 7 8 9 10 11 12 |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
-------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'] <class> </class> |
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:
1 2 3 4 5 6 |
# Get unique values using unique() method deptUniqList = emp_df.Department.unique().tolist() print("-------Unique Department values-------\n",deptUniqList) print(type(deptUniqList)) |
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
-------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'] <class> </class> |
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.
1 2 3 4 5 6 7 8 9 10 11 12 |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
-------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 <class> </class> |
As you can see, we got Series as output by using drop_duplicates()
method.