Truls Møller
Truls Møller

Reputation: 23

How to sort a pandas Series on values while randomizing the order of ties?

I'm using sort_values() to sort values in a pandas Series from largest to smallest. I wonder if there is an easy way of randomizing the order of ties(?). It appears that the indexes of ties come in the descending order given as argument in this case:

s = pd.Series([3.0, 15.0, 1.0, 22.0, 11.0, 12.0, 2.0, 5.0, 3.0, 12.0, 2.0, 3.0])
s.sort_values(ascending=False)

Out:

3     22.0
1     15.0
9     12.0
5     12.0
4     11.0
7      5.0
11     3.0
8      3.0
0      3.0
10     2.0
6      2.0
2      1.0

I have read the documentation that there are different kinds of sort that can be given as argument: ‘quicksort’, ‘mergesort’, ‘heapsort’. However, as far as I understand none of them will randomize the order of ties.

I guess I could write a custom function, but curious to know if something exists.

Upvotes: 2

Views: 510

Answers (1)

Stef
Stef

Reputation: 30609

You can first shuffle the series by taking a random 100 % sample and then sort it:

s.sample(frac=1).sort_values(ascending=False)

You can also pass the random_state argument to start off the random number generator as needed.

Upvotes: 4

Related Questions