Find the unique values in a column and then sort them
--------------------------------------------------
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------
Music by Eric Matyas
https://www.soundimage.org
Track title: RPG Blues Looping
--
Chapters
00:00 Find The Unique Values In A Column And Then Sort Them
00:24 Accepted Answer Score 382
00:45 Answer 2 Score 50
01:12 Answer 3 Score 37
01:29 Answer 4 Score 21
01:44 Answer 5 Score 15
02:15 Thank you
--
Full question
https://stackoverflow.com/questions/3207...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #pandas #sorting #dataframe #unique
#avk47
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------
Music by Eric Matyas
https://www.soundimage.org
Track title: RPG Blues Looping
--
Chapters
00:00 Find The Unique Values In A Column And Then Sort Them
00:24 Accepted Answer Score 382
00:45 Answer 2 Score 50
01:12 Answer 3 Score 37
01:29 Answer 4 Score 21
01:44 Answer 5 Score 15
02:15 Thank you
--
Full question
https://stackoverflow.com/questions/3207...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #pandas #sorting #dataframe #unique
#avk47
ACCEPTED ANSWER
Score 406
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]
ANSWER 2
Score 54
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()
a
Out[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]
ANSWER 3
Score 42
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
ANSWER 4
Score 17
Came across the question myself today. I think the reason that your code returns 'None' (exactly what I got by using the same method) is that
a.sort()
is calling the sort function to mutate the list a. In my understanding, this is a modification command. To see the result you have to use print(a).
My solution, as I tried to keep everything in pandas:
pd.Series(df['A'].unique()).sort_values()