How to get Unique Values of a Column in Pandas DataFrame?

Pandas DataFrame – Get Unique Values in a Column

To get unique values in a column in DataFrame,

  1. Access the column using indexing mechanism DataFrame['columName']. This expression returns the column as Series object.
  2. Call pandas.unique() and pass the Series object obtained in the above step, as argument.

Syntax

The syntax to get unique values of column col in a DataFrame df is

pandas.unique(df['col'])

Examples

1. Get unique values in a specified column

In this example, we take a DataFrame with two columns: name, age; and get the unique values of the column age.

Python Program

import pandas as pd

df = pd.DataFrame({'name':['a', 'b', 'c', 'd', 'e'],
                   'age' :[20, 21, 20, 19, 21]})
result = pd.unique(df['age'])
print(result)
Run Code

Output

[20 21 19]

Summary

In this Pandas Tutorial, we learned how to get the unique values of a column in DataFrame.

Related Tutorials

Privacy Policy Terms of Use

SitemapContact Us