Home > Backend Development > Python Tutorial > How to Sort a Pandas DataFrame by Multiple Columns in Ascending and Descending Order?

How to Sort a Pandas DataFrame by Multiple Columns in Ascending and Descending Order?

Linda Hamilton
Release: 2024-12-15 08:49:09
Original
185 people have browsed it

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])
Copy after login

An equivalent way using the deprecated sort method is:

df.sort(['a', 'b'], ascending=[True, False])
Copy after login

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'])
Copy after login

Sorting this dataframe by 'a' in ascending order and 'b' in descending order gives:

df1.sort(['a', 'b'], ascending=[True, False])
Copy after login
   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
Copy after login

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])
Copy after login

or

df1.sort(['a', 'b'], ascending=[True, False], inplace=True)
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template