Sorting the Columns of Your DataFrame
Sorting the Columns of Your DataFrame. You can also use the column labels of your DataFrame to sort row values. Using
.sort_index() with the optional parameter
axis set to
1 will sort the DataFrame by the column labels.
The sorting algorithm is applied to the axis labels instead of to the actual data. This can be helpful for visual inspections of the DataFrame. When you use
.sort_index() without passing any explicit arguments, it uses
axis=0 as a default argument.
The axis of a DataFrame refers to either the index,
axis=0, or the columns,
axis=1. You can use both axes for indexing and selecting data in a DataFrame, as well as for sorting the data.
1 sorts the columns of your DataFrame based on the column labels.
The columns of your DataFrame are sorted from left to right in ascending alphabetical order. If you want to sort the columns in descending order, then you can use
.sort_index(), you sorted the columns of your DataFrame in both ascending and descending order. This could be more useful in other datasets, such as one in which the column labels correspond to months of the year.
01:32 In that case, it would make sense to arrange your data in ascending or descending order by month. In the next section of the course, you’ll take a look at a common problem: how to approach missing data when sorting in pandas.
Become a Member to join the conversation.