Table of Contents
To find and sort unique values in a column using pandas and python, you can use the unique() function with the sort_values() function. The unique() function will return the unique values in the column, while the sort_values() function will sort them in either ascending or descending order. For example, df[‘column_name’].unique().sort_values(ascending=False) can be used to return the unique values in a column sorted in descending order.
You can use the following basic syntax to find the unique values in a column of a pandas DataFrame and then sort them:
df['my_column'].drop_duplicates().sort_values()
This will return a pandas Series that contains each unique value in a column sorted in ascending order.
To instead sort the unique values in descending order, use ascending=False:
df['my_column'].drop_duplicates().sort_values(ascending=False)
The following example shows how to use this syntax in practice.
Example: Find Unique Values in Pandas Column and Sort Them
Suppose we have the following pandas DataFrame:
import pandas as pd #create DataFrame df = pd.DataFrame({'team': ['A', 'A', 'A', 'A', 'A', 'B', 'B', 'B', 'B', 'B'], 'points': [5, 5, 9, 12, 12, 5, 10, 13, 13, 19]}) #view DataFrame print(df) team points 0 A 5 1 A 5 2 A 9 3 A 12 4 A 12 5 B 5 6 B 10 7 B 13 8 B 13 9 B 19
We can use the following syntax to get the unique values from the points column and then sort them in ascending order:
#get unique values in points column and sort them
df['points'].drop_duplicates().sort_values()
0 5
2 9
6 10
3 12
7 13
9 19
Name: points, dtype: int64
The output displays each of the unique values in the points column sorted in ascending order:
- 5
- 9
- 10
- 12
- 13
- 19
We can also get the unique values in the points column sorted in descending order by specifying ascending=False within the sort_values() function:
#get unique values in points column and sort them in descending order
df['points'].drop_duplicates().sort_values(ascending=False)
9 19
7 13
3 12
6 10
2 9
0 5
Name: points, dtype: int64
The output displays each of the unique values in the points column sorted in descending order:
- 19
- 13
- 12
- 10
- 9
- 5
The following tutorials explain how to perform other common functions in pandas: