You can use df.sort_values() to sort a Pandas DataFrame.
In this guide, you’ll see four practical sorting examples:
- Sorting a column in ascending order
- Sorting a column in descending order
- Sorting by multiple columns – Case 1
- Sorting by multiple columns – Case 2
To begin, consider the following dataset about cars:
| Brand | Price | Year |
|---|---|---|
| HH | 22000 | 2015 |
| TT | 25000 | 2013 |
| FF | 27000 | 2018 |
| AA | 35000 | 2018 |
Create the DataFrame in Python:
import pandas as pd
data = {
'Brand': ['HH', 'TT', 'FF', 'AA'],
'Price': [22000, 25000, 27000, 35000],
'Year': [2015, 2013, 2018, 2018]
}
df = pd.DataFrame(data)
print(df)
Output:
Brand Price Year
0 HH 22000 2015
1 TT 25000 2013
2 FF 27000 2018
3 AA 35000 2018
Now let’s sort the DataFrame using different approaches.
Example 1: Sort Pandas DataFrame in Ascending Order
Suppose you want to sort the DataFrame by the Brand column in ascending order.
By default, sorting is ascending unless specified otherwise.
import pandas as pd
data = {
'Brand': ['HH', 'TT', 'FF', 'AA'],
'Price': [22000, 25000, 27000, 35000],
'Year': [2015, 2013, 2018, 2018]
}
df = pd.DataFrame(data)
df.sort_values(by=['Brand'], inplace=True)
print(df)
Result:
Brand Price Year
3 AA 35000 2018
2 FF 27000 2018
0 HH 22000 2015
1 TT 25000 2013
The brands are now arranged alphabetically.
Example 2: Sort Pandas DataFrame in Descending Order
To sort in descending order, add ascending=False.
import pandas as pd
data = {
'Brand': ['HH', 'TT', 'FF', 'AA'],
'Price': [22000, 25000, 27000, 35000],
'Year': [2015, 2013, 2018, 2018]
}
df = pd.DataFrame(data)
df.sort_values(by=['Brand'], ascending=False, inplace=True)
print(df)
Result:
Brand Price Year
1 TT 25000 2013
0 HH 22000 2015
2 FF 27000 2018
3 AA 35000 2018
The brands are now sorted in reverse alphabetical order.
Example 3: Sort Pandas DataFrame by Multiple Columns – Case 1
You can also sort by more than one column.
Suppose you want to sort first by Year, and then by Price.
import pandas as pd
data = {
'Brand': ['HH', 'TT', 'FF', 'AA'],
'Price': [22000, 25000, 27000, 35000],
'Year': [2015, 2013, 2018, 2018]
}
df = pd.DataFrame(data)
df.sort_values(by=['Year', 'Price'], inplace=True)
print(df)
Result:
Brand Price Year
1 TT 25000 2013
0 HH 22000 2015
2 FF 27000 2018
3 AA 35000 2018
Here, Year has priority because it appears first in the list. For records with the same year (2018), sorting is then applied to Price.
Example 4: Sort by Multiple Columns – Case 2
Now sort by Year and then by Brand:
import pandas as pd
data = {
'Brand': ['HH', 'TT', 'FF', 'AA'],
'Price': [22000, 25000, 27000, 35000],
'Year': [2015, 2013, 2018, 2018]
}
df = pd.DataFrame(data)
df.sort_values(by=['Year', 'Brand'], inplace=True)
print(df)
Result:
Brand Price Year
1 TT 25000 2013
0 HH 22000 2015
3 AA 35000 2018
2 FF 27000 2018
For the year 2018, sorting now follows alphabetical order of the Brand column, so AA appears before FF.
Conclusion
You have seen how to sort a Pandas DataFrame using:
- Single-column sorting (ascending and descending)
- Multi-column sorting with priority order
Key points:
- Use
df.sort_values()to sort. - Sorting is ascending by default.
- Use
ascending=Falsefor descending order. - When sorting by multiple columns, the first column listed has higher priority.
Sorting is a fundamental operation in data analysis and helps you quickly organize and explore your dataset.
