What does sorting by an axis do?

QuestionsQuestions8 SkillsProIntroduction to PandasAug, 06 2025
0130

Sorting by an axis in a DataFrame organizes the data along a specified axis, either rows or columns.

  • Sorting by rows (axis=0): This sorts the DataFrame based on the index (row labels). For example, if you have a DataFrame and you sort it by its index, the rows will be rearranged in ascending or descending order based on their index values.

  • Sorting by columns (axis=1): This sorts the DataFrame based on the column labels. When you sort by columns, the columns will be rearranged in ascending or descending order based on their names.

Here's an example of sorting by columns in descending order:

df.sort_index(axis=1, ascending=False)

This will rearrange the columns of the DataFrame df in descending order based on their labels.

0 Comments

no data
Be the first to share your comment!