pandas.Series.unique() 是否保留顺序?

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

Does pandas.Series.unique() preserve order?

pythonpandas

提问by moustachio

Simple question I haven't been able to find an answer to yet:

我还没有找到答案的简单问题:

Given a pandas Series, I thinkthe order of values given by Series.unique() is that in which they are first encountered in the series, and notany sort sorted order. I.e.

给定一个Pandas系列,我认为Series.unique() 给出的值的顺序是它们在系列中第一次遇到的顺序,而不是任何排序顺序。IE

from pandas import Series
s = Series(['b','b','b','a','a','b'])

s.unique()
>>> array(['b', 'a'], dtype=object)

This is the behavior I want for my application, but can someone tell me if I'm guaranteed to get this order? The documentation is not clear.

这是我的应用程序想要的行为,但是有人可以告诉我是否可以保证获得此订单吗?文档不清楚。

采纳答案by Retozi

yes this generally holds true. pandas objects have ordered indices and the rows will not reshuffle until you tell them to do so...

是的,这通常是正确的。pandas 对象已排序索引,并且在您告诉它们这样做之前,行不会重新排列...

回答by T.C. Proctor

As of now, the pandas documentation contains this line: Uniques are returned in order of appearance, this does NOT sort.

截至目前,pandas 文档包含这一行:按出现顺序返回唯一值,这不排序。