Series.sort()和Series.order()有什么区别? [英] What's the difference between Series.sort() and Series.order()?

查看:409
本文介绍了Series.sort()和Series.order()有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

s = pd.Series( nr.randint( 0, 10, 5 ), index=nr.randint(0, 10, 5 ) )
s

输出

1    3
7    6
2    0
9    7
1    6

order()按值排序并返回新的系列

order() sorts by value and returns a new Series

s.order()

输出

2    0
1    3
7    6
1    6
9    7

sort看起来也可以按值排序,但是就位:

It looks like sort also sorts by value, but in place:

s.sort()
s

输出

2    0
1    3
7    6
1    6
9    7

这是两种方法之间的唯一区别吗?

Is this the only difference between the two methods?

推荐答案

您的问题:这是( Series.sort就地与Series.order return-new-obj )之间的唯一区别这两种方法?

Your Question: Is this (Series.sort in-place v.s. Series.order return-new-obj) the only difference between the two methods?

简短回答:是.它们在功能上是等效的.

Short Answer: YES. They are functionally equivalent.

更长的答案:

pandas.Series.sort() :更改对象本身(就地排序),但不返回任何内容.

pandas.Series.sort(): change the object itself (in-place sorting), but returns nothing.

按值对值和索引标签进行排序.默认情况下,这是就地排序. Series.order是等效的,但返回一个新的系列.

Sort values and index labels by value. This is an inplace sort by default. Series.order is the equivalent but returns a new Series.

所以

>>> s = pd.Series([3,4,0,3]).sort()
>>> s

不输出任何内容.有关更多详细信息,请参见此处的答案.

outputs nothing. See the answer here for more details.

pandas.Series.order() :不更改对象,而是返回新排序的对象.

pandas.Series.order(): dose not change the object, instead it returns a new sorted object.

按值排序Series对象,维护索引值链接.默认情况下,这将返回新系列. Series.sort是等效的,但是是一种就地方法.

Sorts Series object, by value, maintaining index-value link. This will return a new Series by default. Series.sort is the equivalent but as an inplace method.


AFTER Pandas 0.17.0最终版本(即2015年10月9日之后)

排序的API已已更改,事情变得更加干净和愉快.


AFTER pandas 0.17.0 Final release (i.e. after 2015-10-09)

The API of sorting is changed, things became cleaner and more pleasant.

要按进行排序,Series.sort()Series.order()均已已弃用,由新的

To sort by the values, both Series.sort() and Series.order() are DEPRECATED, replaced by the new Series.sort_values() api, which returns a sorted Series object.

总结更改(摘录自pandas 0.17.0

To summary the changes (excerpt from pandas 0.17.0 doc):

To sort by the values (A * marks items that will show a FutureWarning):

        Previous              |         Replacement
------------------------------|-----------------------------------
* Series.order()              |  Series.sort_values()
* Series.sort()               |  Series.sort_values(inplace=True)
* DataFrame.sort(columns=...) |  DataFrame.sort_values(by=...) 

这篇关于Series.sort()和Series.order()有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆