s = pd.Series( nr.randint( 0, 10, 5 ), index=nr.randint(0, 10, 5 ) )
s
Run Code Online (Sandbox Code Playgroud)
产量
1 3
7 6
2 0
9 7
1 6
Run Code Online (Sandbox Code Playgroud)
order() 按值排序并返回一个新系列
s.order()
Run Code Online (Sandbox Code Playgroud)
产量
2 0
1 3
7 6
1 6
9 7
Run Code Online (Sandbox Code Playgroud)
它看起来sort也按价值排序,但到位:
s.sort()
s
Run Code Online (Sandbox Code Playgroud)
产量
2 0
1 3
7 6
1 6
9 7
Run Code Online (Sandbox Code Playgroud)
这是两种方法的唯一区别吗?
YaO*_*OzI 12
你的问题:这是(Series.sort就地vs Series.orderreturn-new-obj)两种方法之间的唯一区别吗?
简答:是的.它们在功能上是等价的.
更长的答案:
pandas.Series.sort():更改对象本身(就地排序),但不返回任何内容.
按值对值和索引标签进行排序.默认情况下,这是一个就地排序.
Series.order相当于但返回一个新系列.
所以
>>> s = pd.Series([3,4,0,3]).sort()
>>> s
Run Code Online (Sandbox Code Playgroud)
没有输出.有关详细信息,请参阅此处的答案.
pandas.Series.order():剂量不会更改对象,而是返回一个新的已排序对象.
按值排序Series对象,维护索引值链接.这将默认返回一个新系列.
Series.sort是等价的,但作为一种原位方法.
排序的API 改变了,事情变得更清洁,更愉快.
由排序值,都Series.sort()和Series.order()被弃用,在更换新的Series.sort_values()API,它返回一个有序系列的对象.
总结变化(摘自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=...)
Run Code Online (Sandbox Code Playgroud)
查看 pandas 源代码(并跳过文档字符串)
def sort(self, axis=0, ascending=True, kind='quicksort', na_position='last', inplace=True):
return self.order(ascending=ascending,
kind=kind,
na_position=na_position,
inplace=inplace)
Run Code Online (Sandbox Code Playgroud)
将其与声明行进行比较(我使用的是 0.14.1)
def order(self, na_last=None, ascending=True, kind='quicksort', na_position='last', inplace=False)
Run Code Online (Sandbox Code Playgroud)
您可以看到,由于 sort 调用了 order 函数,因此除了默认参数之外,两者在所有意图和目的上都是相同的。
正如问题中所指出的,inplace排序inplace = True和顺序参数的默认值inplace = False是不同的,但行为上没有其他差异。
另一个唯一的区别是 order 有一个附加的(但已弃用的)参数,na_last您不能将其与 sort 一起使用(并且无论如何也不应该使用)。
| 归档时间: |
|
| 查看次数: |
6599 次 |
| 最近记录: |