0
0
PandasHow-ToBeginner · 3 min read

How to Use sort_values in pandas for Data Sorting

Use DataFrame.sort_values() to sort a pandas DataFrame by one or more columns. You specify the column(s) with the by parameter and control ascending or descending order with ascending.
📐

Syntax

The sort_values() method sorts a DataFrame by specified column(s). Key parts include:

  • by: Column name or list of columns to sort by.
  • ascending: Boolean or list of booleans to set ascending (True) or descending (False) order.
  • inplace: If True, modifies the original DataFrame; otherwise returns a sorted copy.
python
DataFrame.sort_values(by, ascending=True, inplace=False, ignore_index=False)
💻

Example

This example shows sorting a DataFrame by one column ascending and by two columns with mixed order.

python
import pandas as pd

data = {'Name': ['Alice', 'Bob', 'Charlie', 'David'],
        'Age': [25, 30, 25, 35],
        'Score': [85, 90, 85, 80]}
df = pd.DataFrame(data)

# Sort by Age ascending
sorted_age = df.sort_values(by='Age')

# Sort by Age ascending, then Score descending
sorted_age_score = df.sort_values(by=['Age', 'Score'], ascending=[True, False])

print('Sorted by Age:')
print(sorted_age)
print('\nSorted by Age and Score:')
print(sorted_age_score)
Output
Sorted by Age: Name Age Score 0 Alice 25 85 2 Charlie 25 85 1 Bob 30 90 3 David 35 80 Sorted by Age and Score: Name Age Score 0 Alice 25 85 2 Charlie 25 85 1 Bob 30 90 3 David 35 80
⚠️

Common Pitfalls

Common mistakes include:

  • Forgetting by parameter causes error.
  • Not setting ascending correctly when sorting by multiple columns.
  • Expecting the original DataFrame to change without inplace=True.

Example of wrong and right usage:

python
# Wrong: missing 'by' parameter
# df.sort_values()

# Wrong: ascending list length mismatch
# df.sort_values(by=['Age', 'Score'], ascending=[True])

# Right: correct ascending list length
sorted_correct = df.sort_values(by=['Age', 'Score'], ascending=[True, False])
print(sorted_correct)
Output
Name Age Score 0 Alice 25 85 2 Charlie 25 85 1 Bob 30 90 3 David 35 80
📊

Quick Reference

ParameterDescriptionDefault
byColumn name or list of columns to sort byNone (required)
ascendingSort ascending if True, descending if False; can be list for multiple columnsTrue
inplaceModify original DataFrame if True, else return sorted copyFalse
ignore_indexReset index in result if TrueFalse
na_positionPosition of NaNs: 'last' or 'first''last'

Key Takeaways

Use sort_values(by=...) to sort DataFrames by one or more columns.
Set ascending=True or False (or list) to control sort order per column.
Use inplace=True to modify the original DataFrame directly.
Always provide the 'by' parameter; it is required.
For multiple columns, ensure ascending list matches the number of columns.