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