Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does pandas.Series.unique() preserve order?

Tags:

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

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

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.

like image 309
moustachio Avatar asked Apr 09 '14 19:04

moustachio


People also ask

Does unique preserve order?

Method 7 – pandas unique() pandas is better suited to the task because it preserves order by default and pd.

What does unique () do in pandas?

The unique function in pandas is used to find the unique values from a series. A series is a single column of a data frame. We can use the unique function on any possible set of elements in Python. It can be used on a series of strings, integers, tuples, or mixed elements.

Does pandas DataFrame preserve order?

Pandas. DataFrame doesn't preserve the column order when converting from a DataFrames.

How do you keep unique values in pandas?

You can get unique values in column (multiple columns) from pandas DataFrame using unique() or Series. unique() functions. unique() from Series is used to get unique values from a single column and the other one is used to get from multiple columns.


2 Answers

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

like image 65
Retozi Avatar answered Oct 04 '22 21:10

Retozi


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

like image 33
T.C. Proctor Avatar answered Oct 04 '22 20:10

T.C. Proctor