Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (50.2k points)

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.

2 Answers

0 votes
by (107k points)

 You can apply the sorted function which returns a new sorted list from the items in

iterable. Refer the following 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]

If you want to learn more about Pandas then visit this Python Course designed by the industrial experts.

 

0 votes
by (41.4k points)

You can use this one line code also:

print(sorted(df['Column Name'].unique()))

Related questions

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...