Categories
Python Answers

How to sort a dataFrame in Python Pandas by two or more columns?

Spread the love

To sort a dataFrame in Python Pandas by two or more columns, we call sort_values with the ascending argument.

For instance, we write

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

to call sort_values on the df data frame with an array of columns to sort as the first argument.

And we set ascending to an array of booleans to sort the columns in the order listed for each column.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *