


How to Sort a Pandas DataFrame by Multiple Columns in Ascending and Descending Order?
Sorting a Pandas Dataframe by Multiple Columns
Sorting a Pandas dataframe by multiple columns is a common operation in data analysis. Consider a dataframe with columns 'a', 'b', and 'c'. To sort this dataframe by column 'b' in ascending order and column 'c' in descending order, follow these steps:
Starting from Pandas version 0.17.0, the sort method has been deprecated in favor of sort_values. As of version 0.20.0, sort has been completely removed. However, the arguments and results remain unchanged:
df.sort_values(['a', 'b'], ascending=[True, False])
An equivalent way using the deprecated sort method is:
df.sort(['a', 'b'], ascending=[True, False])
For example, consider a dataframe df1 with random integer values in columns 'a' and 'b':
import pandas as pd import numpy as np df1 = pd.DataFrame(np.random.randint(1, 5, (10, 2)), columns=['a', 'b'])
Sorting this dataframe by 'a' in ascending order and 'b' in descending order gives:
df1.sort(['a', 'b'], ascending=[True, False])
a b 2 1 4 7 1 3 1 1 2 3 1 2 4 3 2 6 4 4 0 4 3 9 4 3 5 4 1 8 4 1
Remember that the sort method is not in-place by default. To update df1 with the sorted values, assign the result of the sort method to df1 or use inplace=True in the method call:
df1 = df1.sort(['a', 'b'], ascending=[True, False])
or
df1.sort(['a', 'b'], ascending=[True, False], inplace=True)
The above is the detailed content of How to Sort a Pandas DataFrame by Multiple Columns in Ascending and Descending Order?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

How to avoid being detected when using FiddlerEverywhere for man-in-the-middle readings When you use FiddlerEverywhere...

Fastapi ...

Using python in Linux terminal...

How to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...

About Pythonasyncio...

Understanding the anti-crawling strategy of Investing.com Many people often try to crawl news data from Investing.com (https://cn.investing.com/news/latest-news)...

Loading pickle file in Python 3.6 environment error: ModuleNotFoundError:Nomodulenamed...

Discussion on the reasons why pipeline files cannot be written when using Scapy crawlers When learning and using Scapy crawlers for persistent data storage, you may encounter pipeline files...
