Python:Pandas .sort_values()
In Pandas, the .sort_values() function is used to sort values in a DataFrame by one or more columns. This function is useful in data analysis, data visualization, data cleaning, and more.
Syntax
DataFrame.sort_values(by, axis=0, ascending=True, inplace=False, kind='quicksort', na_position='last', ignore_index=False, key=None)
Parameters:
by: A label or a list of labels to sort by (levels can also be specified if applicable).axis: Specifies which axis will be sorted (0or index,1or columns); defaults to0.ascending: Specifies whether the sort will be ascending or descending (TrueorFalse); defaults toTrue.inplace: By setting it toTrue, the operation will be performed on the originalDataFrameand the function will returnNone; defaults toFalse.kind: Specifies which algorithm to use:'quicksort','mergesort','heapsort', or'stable'; defaults to'quicksort'.na_position: Specifies where to put theNaNvalues; defaults tolast.ignore_index: Ignores the original index and put a new ascending set of values in its place. The default isFalse.key: Applies a function before sorting.
Return value:
The .sort_values() function returns a sorted DataFrame (or None if inplace=True).
Example 1: Sort by a Single Column
In this example, the DataFrame is sorted in ascending order based on the Score column:
import pandas as pddata = {'Name': ['Alice', 'Bob', 'Charlie', 'David'],'Score': [88, 92, 85, 90]}df = pd.DataFrame(data)sorted_df = df.sort_values(by='Score')print(sorted_df)
Here is the output:
Name Score2 Charlie 850 Alice 883 David 901 Bob 92
Example 2: Sort by Multiple Columns
In this example, the data is first sorted by Department, and then by Score within each department:
import pandas as pddata = {'Name': ['Alice', 'Bob', 'Charlie', 'David', 'Eve'],'Department': ['HR', 'Finance', 'HR', 'Finance', 'HR'],'Score': [88, 92, 85, 90, 85]}df = pd.DataFrame(data)sorted_df = df.sort_values(by=['Department', 'Score'])print(sorted_df)
Here is the output:
Name Department Score3 David Finance 901 Bob Finance 922 Charlie HR 854 Eve HR 850 Alice HR 88
Codebyte Example: Sort in Descending Order with NaNs First
In this codebyte example, missing values (NaNs) appear at the top, and the Price column is sorted in descending order:
Frequently Asked Questions
1. What’s the difference between .sort_values() and .sort_index()?
.sort_values() sorts by column values, while .sort_index() sorts by row or column index. Use .sort_index() when sorting based on DataFrame labels rather than content.
2. Does .sort_values() modify the original DataFrame?
By default, .sort_values() returns a new DataFrame. To modify the original, set inplace=True.
3. Can I sort a Series using .sort_values()?
Yes. Pandas Series also supports .sort_values(), and the syntax is simpler since there’s no need to specify a column.
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Python:Pandas on Codecademy
- Machine Learning Data Scientists solve problems at scale, make predictions, find patterns, and more! They use Python, SQL, and algorithms.
- Includes 27 Courses
- With Professional Certification
- Beginner Friendly.95 hours
- Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
- With Certificate
- Beginner Friendly.24 hours