
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Write a Python program to sort a given DataFrame by name column in descending order
Input −
Assume, sample DataFrame is,
Id Name 0 1 Adam 1 2 Michael 2 3 David 3 4 Jack 4 5 Peter
Output −
After, sorting the elements in descending order as,
Id Name 4 5 Peter 1 2 Michael 3 4 Jack 2 3 David 0 1 Adam
Solution
To solve this, we will follow the below approaches.
Define a DataFrame
Apply DataFrame sort_values method based on Name column and add argument ascending=False to show the data in descending order. It is defined below,
df.sort_values(by='Name',ascending=False)
Example
Let us see the following implementation to get a better understanding.
import pandas as pd data = {'Id': [1,2,3,4,5],'Name': ['Adam','Michael','David','Jack','Peter']} df = pd.DataFrame(data) print("Before sorting:\n", df) print("After sorting:\n", df.sort_values(by='Name',ascending=False))
Output
Before sorting: Id Name 0 1 Adam 1 2 Michael 2 3 David 3 4 Jack 4 5 Peter After sorting: Id Name 4 5 Peter 1 2 Michael 3 4 Jack 2 3 David 0 1 Adam
- Related Articles
- Write a program in Python to sort all the elements in a given series in descending order
- Python – Descending Order Sort grouped Pandas dataframe by group size?
- Python Pandas - Sort DataFrame in descending order according to the element frequency
- Write a program in Python to perform flatten the records in a given dataframe by C and F order
- Write a program in Python to filter City column elements by removing the unique prefix in a given dataframe
- Sort a column in descending order after placing argument in MySQL IN()?
- Write a program in Python to modify the diagonal of a given DataFrame by 1
- Python program to sort the elements of an array in descending order
- C# program to sort an array in descending order
- 8085 Program to perform bubble sort in descending order
- 8085 Program to perform selection sort in descending order
- C program to sort an array in descending order
- Write a Python program to reshape a given dataframe in different ways
- Golang Program to sort an array in descending order using insertion sort
- Swift Program to sort an array in descending order using bubble sort

Advertisements