HOWTO · Pandas
How to Get Pandas Unique Values in Column and Sort Them
This tutorial demonstrates how to find unique values of a column in Pandas DataFrame and how to sort it
On this page
A DataFrame column is a Pandas Series. Call its unique() function to get the distinct values in the order they first appear. Use drop_duplicates() when you need the corresponding rows and their other columns, or sort the values or rows with Python’s sorted() or Pandas sort_values().
The examples use one small DataFrame of professions and departments. Run this example:
import pandas as pd
df = pd.DataFrame({
"profession": ["designer", "engineer", "designer", "analyst"],
"department": ["product", "platform", "research", "finance"],
})
professions = df["profession"].unique()
print(professions)
print(type(professions).__name__)
<StringArray>
['designer', 'engineer', 'analyst']
Length: 3, dtype: str
StringArray
Get Unique Values From a DataFrame Column
Selecting df["profession"] returns a Series. Its unique() function returns one value for each distinct profession, in first-seen order; it does not sort the values. The concrete result type depends on the Series dtype. In this example the values use Pandas’ string extension dtype, so the result is a StringArray, as shown in the output. Other dtypes can produce a NumPy array or a different extension array. Adapt the column name to your data. If you need only the number of distinct values, use len(df["profession"].unique()); len(df) instead counts rows, including rows with repeated professions. Convert the result with .tolist() if the next step specifically needs a regular Python list.
Keep One Row for Each Unique Column Value
When the task is to retain records rather than produce only a collection of values, call drop_duplicates(subset="profession") on the DataFrame. The result is still a DataFrame: each retained row includes its department, and the first row for each profession is kept by default. In the displayed result, the second designer row is removed while the first designer row and its product department remain. This does not select a representative row based on another rule; sort or otherwise choose rows first if a different record should be retained. By default, the result keeps the original index labels, so its index may have gaps. You can pass several column names in a list to subset when duplicate records are defined by a combination of columns.
import pandas as pd
df = pd.DataFrame({
"profession": ["designer", "engineer", "designer", "analyst"],
"department": ["product", "platform", "research", "finance"],
})
unique_rows = df.drop_duplicates(subset="profession")
print(unique_rows.to_string(index=False))
print(type(unique_rows).__name__)
profession department
designer product
engineer platform
analyst finance
DataFrame
Sort Unique Values or DataFrame Rows
Python’s built-in sorted() returns a new list of values in ascending order. To sort the distinct values, compose it with unique(), or sort the Series directly if duplicates should remain. For descending order, pass reverse=True to sorted().
Use sort_values(by="profession") when you want sorted rows and their other columns to remain together. It returns a DataFrame and sorts ascending by default; set ascending=False for descending order. These examples are alternatives: sorting the unique values gives a list, while sorting rows preserves the row data. You can pass a list to by to sort by multiple columns in priority order. The displayed lists contain only profession values, but the final output keeps every row and its department while ordering professions from engineer down to analyst. sort_values() keeps the original index labels unless you request ignore_index=True, which creates a fresh sequential index for the sorted result.
import pandas as pd
df = pd.DataFrame({
"profession": ["designer", "engineer", "designer", "analyst"],
"department": ["product", "platform", "research", "finance"],
})
print(sorted(df["profession"].unique()))
print(sorted(df["profession"].unique(), reverse=True))
print(df.sort_values(by="profession", ascending=False).to_string(index=False))
['analyst', 'designer', 'engineer']
['engineer', 'designer', 'analyst']
profession department
engineer platform
designer product
designer research
analyst finance