How to sort unique values of column?

Screen Link:

I hope I am posting this good. I still do not understand how to link a question with the project…

The thing should be easy, but I cannot find a code to get unique values of a column sorted. Either I get unique values, or I get them sorted. I am getting crazy trying to find out that.

For example, I have a column with this values: [1, 2, 5, 3, 2, 1]. I would like to get the result: [5,3,2,1]

We have this for unique values, but they are not sorted:
dete_resignations[‘institute_service’].unique()

Or this to sort values, but then we have all of them:
dete_resignations[‘institute_service’].sort_values()

I can get values sorted by the number of appearences with:
dete_resignations[‘institute_service’].value_counts().sort_values()

But again, this is not what I want.

Thanks!

Hi @ncirauqu,

You should do this:

sorted_unique = sorted(dete_resignations['institute_service'].unique())
print(sorted_unique)

Or, if you need a list in descending order:

sorted_reverse_unique = sorted(dete_resignations['institute_service'].unique(), reverse=True)
print(sorted_reverse_unique)

That’s it. Thanks so much!!!

1 Like