Find the unique values in a column and then sort them
Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn
--
Music by Eric Matyas
https://www.soundimage.org
Track title: Lost Meadow
--
Chapters
00:00 Question
00:29 Accepted answer (Score 343)
00:50 Answer 2 (Score 44)
01:19 Answer 3 (Score 32)
01:35 Answer 4 (Score 15)
01:50 Thank you
--
Full question
https://stackoverflow.com/questions/3207...
Accepted answer links:
[sorted(iterable)]: https://docs.python.org/3/library/functi...
Answer 2 links:
[sort]: http://pandas.pydata.org/pandas-docs/sta...
Answer 3 links:
[drop_duplicates()]: http://pandas.pydata.org/pandas-docs/sta...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #pandas #sorting #dataframe #unique
#avk47
--
Music by Eric Matyas
https://www.soundimage.org
Track title: Lost Meadow
--
Chapters
00:00 Question
00:29 Accepted answer (Score 343)
00:50 Answer 2 (Score 44)
01:19 Answer 3 (Score 32)
01:35 Answer 4 (Score 15)
01:50 Thank you
--
Full question
https://stackoverflow.com/questions/3207...
Accepted answer links:
[sorted(iterable)]: https://docs.python.org/3/library/functi...
Answer 2 links:
[sort]: http://pandas.pydata.org/pandas-docs/sta...
Answer 3 links:
[drop_duplicates()]: http://pandas.pydata.org/pandas-docs/sta...
--
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()