Fix Python – Find the unique values in a column and then sort them

Question

Asked By – MAS

I have a pandas dataframe. I want to print the unique values of one of its columns in ascending order. This is how I am doing it:

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

The problem is that I am getting a None for the output.

Now we will see solution for issue: Find the unique values in a column and then sort them


Answer

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

CODE

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

OUTPUT

[1, 2, 3, 6, 8]

This question is answered By – Vineet Kumar Doshi

This answer is collected from stackoverflow and reviewed by FixPython community admins, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0