working4coins working4coins - 1 month ago 7
Python Question

Difference between sort and sort_index

Python Pandas provides two methods for sorting DataFrame :



What are differences between these two methods ?

Answer

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 columns
  • sort_index is meant to sort by the index (or a specific level of the index)

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.html#changes-to-sorting-api