Home >Backend Development >Python Tutorial >How Do I Sort a Pandas DataFrame by Multiple Columns?

How Do I Sort a Pandas DataFrame by Multiple Columns?

Susan Sarandon
Susan SarandonOriginal
2024-12-13 17:08:13153browse

How Do I Sort a Pandas DataFrame by Multiple Columns?

Multi-Column Sorting in Pandas

Sorting a Pandas DataFrame based on multiple columns is a common task. This article explains how to perform multi-column sorting using the sort() method, which has been deprecated in favor of sort_values() in recent versions of Pandas.

sort_values() Method:

As of Pandas 0.17.0, the sort_values() method is recommended for multi-column sorting. Its syntax is:

df.sort_values(by=['column_1', 'column_2'], ascending=[True, False])

where:

  • by specifies the columns to sort by
  • ascending specifies the sorting order for each column (True for ascending, False for descending)

For example:

To sort a DataFrame df by column 'b' in ascending order and column 'c' in descending order, you can use:

df = df.sort_values(['b', 'c'], ascending=[True, False])

sort() Method (deprecated):

For Pandas versions prior to 0.20.0, you can still use the sort() method with the ascending argument:

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

However, it's important to note that the sort() method is not inplace by default. Therefore, you should assign the result of the sort operation to a new DataFrame or use inplace=True to replace the original DataFrame with the sorted version:

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 Do I Sort a Pandas DataFrame by Multiple Columns?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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