pandas sort_values 和 sort_index 有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19332171/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the difference between sort_values and sort_index?
提问by working4coins
Python Pandas provides two methods for sorting DataFrame :
Python Pandas 提供了两种排序 DataFrame 的方法:
- sort_values(or DEPRECATED sort)
- sort_index
- sort_values(或已弃用的排序)
- 排序索引
What are differences between these two methods ?
这两种方法有什么区别?
采纳答案by joris
As the question was updated to ask for the difference between sort_values(as sortis 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(as sortis deprecated) 和之间的区别sort_index,@mathdan 的答案不再反映最新 Pandas 版本 (>= 0.17.0) 的当前状态。
sort_valuesis meant to sort by the values of columnssort_indexis meant to sort by the index labels(or a specific level of the index, or the column labels whenaxis=1)
sort_values旨在按列的值排序sort_index旨在按索引标签(或索引的特定级别,或列标签axis=1)排序
Previously, sort(deprecated starting from pandas 0.17.0) and sort_indexwhere 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
以前,sort(从 pandas 0.17.0 开始已弃用)并且sort_index实际上几乎相同(两种方法都可以按列和索引进行排序)。但是这种令人困惑的情况在 0.17.0 中得到了解决。
有关排序 API 更改的概述,请参阅http://pandas.pydata.org/pandas-docs/stable/whatsnew/v0.17.0.html#changes-to-sorting-api
回答by mathdan
The difference is entirely in the way it is called. The source codefor sortis literally a one-line call to sort_index.
区别完全在于它的调用方式。的源代码为sort是字面上一个单行调用sort_index。

