


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

AI Hentai Generator
Generate AI Hentai for free.

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



Solution to permission issues when viewing Python version in Linux terminal When you try to view Python version in Linux terminal, enter python...

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...

When using Python's pandas library, how to copy whole columns between two DataFrames with different structures is a common problem. Suppose we have two Dats...

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

Regular expressions are powerful tools for pattern matching and text manipulation in programming, enhancing efficiency in text processing across various applications.

How does Uvicorn continuously listen for HTTP requests? Uvicorn is a lightweight web server based on ASGI. One of its core functions is to listen for HTTP requests and proceed...

The article discusses popular Python libraries like NumPy, Pandas, Matplotlib, Scikit-learn, TensorFlow, Django, Flask, and Requests, detailing their uses in scientific computing, data analysis, visualization, machine learning, web development, and H

In Python, how to dynamically create an object through a string and call its methods? This is a common programming requirement, especially if it needs to be configured or run...
