Answer a question

Python Pandas provides two methods for sorting DataFrame :

  • sort_values (or DEPRECATED sort)
  • sort_index

What are differences between these two methods ?

Answers

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 columns
  • sort_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

Logo

Python社区为您提供最前沿的新闻资讯和知识内容

更多推荐