Find the unique values in a column and then sort them Find the unique values in a column and then sort them python python

Find the unique values in a column and then sort them


sorted(iterable): Return a new sorted list from the items in iterable.

CODE

import pandas as pddf = pd.DataFrame({'A':[1,1,3,2,6,2,8]})a = df['A'].unique()print(sorted(a))

OUTPUT

[1, 2, 3, 6, 8]


sort sorts inplace so returns nothing:

In [54]:df = pd.DataFrame({'A':[1,1,3,2,6,2,8]})a = df['A'].unique()a.sort()aOut[54]:array([1, 2, 3, 6, 8], dtype=int64)

So you have to call print a again after the call to sort.

Eg.:

In [55]:df = pd.DataFrame({'A':[1,1,3,2,6,2,8]})a = df['A'].unique()a.sort()print(a)[1 2 3 6 8]


You can also use the drop_duplicates() instead of unique()

df = pd.DataFrame({'A':[1,1,3,2,6,2,8]})a = df['A'].drop_duplicates()a.sort()print a