gpt4 book ai didi

python - Series.sort() 和 Series.order() 有什么区别?

转载 作者:太空狗 更新时间:2023-10-29 18:02:21 25 4
gpt4 key购买 nike

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() 按值排序并返回一个新系列

s.order()

输出

2    0
1 3
7 6
1 6
9 7

看起来 sort 也按值排序,但在适当的地方:

s.sort()
s

输出

2    0
1 3
7 6
1 6
9 7

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

最佳答案

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

在 pandas 0.17.0 最终版本之前(即在 2015-10-09 之前)

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

更长的答案:

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

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

什么都不输出。参见 the answer here了解更多详情。

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

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.


在 pandas 0.17.0 最终版本之后(即 2015-10-09 之后)

排序的API是changed ,事情变得更干净,更愉快。

要按 排序,Series.sort()Series.order() 都是已弃用,替换为新的 Series.sort_values() api,它返回一个排序的 Series 对象。

总结变化(摘自 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=...)

关于python - Series.sort() 和 Series.order() 有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23696834/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com