Sorting Columns and Indexes
Explore how to sort columns and index values.
We'll cover the following...
Sorting columns
The sort_values
method allows us to sort the rows of a DataFrame by arbitrary columns. In this example, we sort our DataFrame by the political party in alphabetical order:
Press + to interact
print(pres.sort_values(by='Party'))
We can also sort by multiple columns, and we can specify whether each column should be sorted in ascending (the default) or descending order. Here we sort by the Party
column in ascending alphabetic order and ...