Python Pandas provides two methods for sorting DataFrame :
- sort_values (or DEPRECATED sort)
- sort_index
What are differences between these two methods ?
Python Pandas provides two methods for sorting DataFrame :
What are differences between these two methods ?
As the question was updated to ask for the difference between sort_values
(as sort
is deprecated) and sort_index
, the answer of @mathdan is no longer reflecting the current state with the latest pandas version (>= 0.17.0).
sort_values
is meant to sort by the values of columnssort_index
is meant to sort by the index labels (or a specific level of the index, or the column labels when axis=1
)Previously, sort
(deprecated starting from pandas 0.17.0) and sort_index
where indeed almost identical (both methods could sort by both columns and index). But this confusing situation has been solved in 0.17.0.
For an overview of the changes in the sorting API, see http://pandas.pydata.org/pandas-docs/stable/whatsnew/v0.17.0.html#changes-to-sorting-api
The difference is entirely in the way it is called. The source code for sort
is literally a one-line call to sort_index
.