Python 为什么在 Pandas Series 上调用 .sort() 函数会就地对其值进行排序并且不返回任何内容?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24049509/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 03:53:28  来源:igfitidea点击:

Why calling .sort() function on Pandas Series sorts its values in-place and returns nothing?

pythonpython-3.xpandas

提问by meto

Sorry I think I am missing something very basic here:

对不起,我想我在这里遗漏了一些非常基本的东西:

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

outputs None, while

输出无,而

>>> Series([3,4,0,3]).order()
2    0
0    3
3    3
1    4
dtype: int64

what am I missing with sort()?

我缺少 sort() 什么?

Thanks

谢谢

EDIT:

编辑:

Thanks for the answers, I do realize now that this is sorting in place. But I don't understand why

感谢您的回答,我现在意识到这是在排序。但我不明白为什么

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

does not return the sorted Series. If I understand the manualit should return the series sorted in place.

不返回排序的系列。如果我理解手册,它应该返回排序到位的系列。

采纳答案by gerrit

.sort()sorts in-place.

.sort()就地排序。

That means that after you call .sort(), your existing arrayhas been sorted. It doesn't return anything.

这意味着在您调用 之后.sort(),您现有的数组已被排序。它不返回任何东西。

To take an example from "core" Python:

以“核心”Python 为例:

In [175]: L = [2, 3, 1, 5]

In [176]: L.sort()

In [177]: print(L)
[1, 2, 3, 5]

It's the same for Pandas, as documented by Pandas.sort:

Pandas 也是如此,如Pandas.sort 所述

Sort values and index labels by value, in place. For compatibility with ndarray API. No return value

就地按值对值和索引标签进行排序。为了与 ndarray API 兼容。无返回值

See also: What's the difference between Series.sort() and Series.order()?

另请参阅:Series.sort() 和 Series.order() 之间有什么区别?

回答by Christian Geier

As with most other python iterables, Series.sort() does actually return nothing but sorts the Series in place. See for example sorting a python list:

与大多数其他 python 迭代器一样, Series.sort() 实际上不返回任何内容,只是对系列进行了排序。例如,请参见对 Python 列表进行排序:

In [2]: foo = [5, 3, 9]

In [3]: foo.sort()

In [4]: foo
Out[4]: [3, 5, 9]

回答by YaOzI

In [1]: import pandas as pd
In [2]: s = pd.Series([3,4,0,3]).sort()
In [3]: s

Indeed In [3]will output nothing, as you can check:

IndeedIn [3]不会输出任何内容,您可以检查:

In [4]: type(s)
Out[4]: NoneType

The reason:

原因:

pd.Series([3,4,0,3])indeed return a pandas Seriestype object, BUT Series.sort()method return nothingbecause of inplacesorting. So the expression s = pd.Series([3,4,0,3]).sort(), sin LHS get nothing from RHS, thus In [3]: soutput nothing.

pd.Series([3,4,0,3])确实返回一个 PandasSeries类型的对象,但由于就地排序,Series.sort()方法不返回任何内容。所以表达式,在 LHS 从 RHS 什么也得不到,因此什么都不输出。s = pd.Series([3,4,0,3]).sort()sIn [3]: s

NOTE that:

注意:

After version 0.17.0, sorting by valuemethods pandas.Series.sort()and pandas.Series.order()are DEPRECATED, replaced by a unified pandas.Series.sort_values()API. See this answerfor more details.

版本之后0.17.0或更新版本,通过排序的方法pandas.Series.sort(),并pandas.Series.order()已被弃用,取而代之的是一个统一的pandas.Series.sort_values()API。有关更多详细信息,请参阅此答案

回答by Aniruddha Kalburgi

Both .sort()and order()functions are DEPRECATED

.sort_values()function is the replacement and here's the example on how to use it.

两者的.sort()顺序()函数不推荐使用

.sort_values()函数是更换,这里是如何使用它的例子。

Example:

例子:

import numpy as np
import pandas as pd

arr = np.array([1,3,4,2])
series = pd.Series(arr)

Ascending Order
Equivalent to .order() function from old versions.

升序
相当于旧版本的 .order() 函数。

ascending = series.sort_values() 


Descending Order
Equivalent to .order(ascending=False)


降序
等效于 .order(ascending=False)

descending = series.sort_values(ascending=False)


In Place
Equivalent to .sort() from old versions.


就地
相当于旧版本的 .sort() 。

series.sort_values(inplace=True) 

For more info, check official documentation here

有关更多信息,请在此处查看官方文档