Sorting Data
Sorting helps you organize and analyze your data. Pandas provides flexible sorting methods.
Sorting by Single Column
Use sort_values() to sort by a column:
Loading Pandas Playground...
Sorting by Multiple Columns
Sort by multiple columns with different orders:
Loading Pandas Playground...
Sorting by Index
Sort by the row index:
Loading Pandas Playground...
Handling Missing Values
Control where NaN values appear:
Loading Pandas Playground...
In-Place Sorting
Modify the original DataFrame:
Loading Pandas Playground...
Sorting Columns
Sort columns instead of rows:
Loading Pandas Playground...
Exercise: Sort by Score
Loading Exercise...
Exercise: Multi-Column Sort
Loading Exercise...
Key Points
sort_values()sorts by column valuessort_index()sorts by indexascending=Falsefor descending order- Pass lists to sort by multiple columns
na_position='first'or'last'for NaN placementinplace=Truemodifies the original DataFrameaxis=1to sort columns

