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.
.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.