Pandas >> How to Sort in Pandas
Table of Contents
In this article, we will talk about how to sort data in DataFrame
.
Sometimes we want to display data in some order. We can use sort functions of pandas to achieve this. h Firstly, we prepare data for demonstrating the sort function.
Preparing data
import pandas as pd
df = pd.DataFrame({
"name": ["Kevin", "Jack", "Mary", "Bob", "Robert", "Amy"],
"score": [80, 90, 95, 93, 88, 81],
"class": ["A", "B", "A", "A", "B", "B"]
}, index=["K", "J", "M", "B", "R", "A"])
df
Result
name | score | class | |
---|---|---|---|
K | Kevin | 80 | A |
J | Jack | 90 | B |
M | Mary | 95 | A |
B | Bob | 93 | A |
R | Robert | 88 | B |
A | Amy | 81 | B |
Sorting by one column
We can use sort_values
method and specify the column name to sort by a single column.
Attention: sort_values
method will not change origin DataFrame and it will return a sorted DataFrame.
df.sort_values("score")
# OR
df.sort_values(by="score")
Result
name | score | class | |
---|---|---|---|
K | Kevin | 80 | A |
A | Amy | 81 | B |
R | Robert | 88 | B |
J | Jack | 90 | B |
B | Bob | 93 | A |
M | Mary | 95 | A |
We can also specify the sort order by ascending
option.
True is ascending order, False is descending order.
# ascending order
df.sort_values("score", ascending=True)
# descending order
df.sort_values("score", ascending=False)
Sorting by multiple columns
We can specify a list or array of column names to sort by multiple columns.
df.sort_values(by=["class", "score"])
Result
name | score | class | |
---|---|---|---|
K | Kevin | 80 | A |
B | Bob | 93 | A |
M | Mary | 95 | A |
A | Amy | 81 | B |
R | Robert | 88 | B |
J | Jack | 90 | B |
We can use ascending
option to specify sort order for all columns.
df.sort_values(by=["class", "score"], ascending=False)
Result
name | score | class | |
---|---|---|---|
J | Jack | 90 | B |
R | Robert | 88 | B |
A | Amy | 81 | B |
M | Mary | 95 | A |
B | Bob | 93 | A |
K | Kevin | 80 | A |
We can also specify sort order for each column by specifying a list or array of ascending boolean values with same length.
df.sort_values(by=["class", "score"], ascending=[False, True])
Result
name | score | class | |
---|---|---|---|
A | Amy | 81 | B |
R | Robert | 88 | B |
J | Jack | 90 | B |
K | Kevin | 80 | A |
B | Bob | 93 | A |
M | Mary | 95 | A |
Sorting by Index
We can use sort_index method to sort data in DataFrame. This method also don’t change original DataFrame and return the sorted DataFrame.
df.sort_index()
df.sort_index(ascending=False)
If we want to sort in columns direction, we can specify axis
option.
df.sort_index(axis="columns")
# OR
df.sort_index(axis="columns", ascending=True)
# OR
df.sort_index(axis=1)
# OR
df.sort_index(axis=1, ascending=True)
Result
class | name | score | |
---|---|---|---|
K | A | Kevin | 80 |
J | B | Jack | 90 |
M | A | Mary | 95 |
B | A | Bob | 93 |
R | B | Robert | 88 |
A | B | Amy | 81 |