matti0006
matti0006

Reputation: 75

Sort values in dataframe, but randomize order of items with same value

I am writing a recommendation system that recommends products based on a score assigned to each product, for example in the following dataframe:

index   product_name     score
0       prod_1           2
1       prod_2           2
2       prod_3           1
3       prod_4           3

I can of course sort this dataframe by score, using sort_values('score', ascending = False), however, this will always result in the following dataframe:

index   product_name     score
3       prod_4           3
0       prod_1           2
1       prod_2           2
2       prod_3           1

However, I would like to randomly shuffle the order of prod_1 and prod_2, as they have the same score. It doesn't seem like sort_values has any way of achieving this.

The only solution I can come up with is to fetch all possible scores from the dataframe, then make a new dataframe for each score, shuffle those, and then stitch them back together, but it seems like there should be a better way.

Upvotes: 3

Views: 1390

Answers (1)

Just Honza
Just Honza

Reputation: 231

What about a new column with completely random numbers (use e.g. numpy.random.randint) and then sort it by both?

sort_values(by=["score","rand_col"], ascending=[False,False])

Upvotes: 4

Related Questions