How to Get Unique Values in Column of Pandas DataFrame

Previous
Next

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

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.

Output:

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:

Output:

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.

Output:

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

Previous
Next

Add Comment