Sort DataFrame in descending order

  • Post category:Pandas

In this Pandas tutorial, you will learn how to sort DataFrame in descending order. You can easily sort the Pandas DataFrame in descending order with the following code.

# import Pandas library
import pandas as pd

# create a dataframe
var = pd.DataFrame({'A': [10, 5, 40, 20], 'B': [5, 10, 15, 20], 'C': [5, 30, 15, 40]})

# sort the dataframe by column B in descending order
var.sort_values(by=['B'], inplace=True, ascending=False)

# display the dataframe
print(var)

Output:

    A   B   C
3  20  20  40
2  40  15  15
1   5  10  30
0  10   5   5

Free resources to learn advanced skills: AiHints and CodeAllow