领带打结时,大 pandas 如何决定排序? [英] How is pandas deciding order in a sort when there is a tie?

查看:110
本文介绍了领带打结时,大 pandas 如何决定排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

熊猫0.12.0

在下面的DataFrame中,为什么例如使索引混乱?看一下4,索引从1,15,6,7开始.熊猫用来决定如何排序的原因是什么,我可能会怀疑索引在相等值的情况下仍保持顺序.

In the DataFrame below, why for example does it jumble the indexes? Look at the 4's, the indexes go from 1,15,6,7. What is the reasoning pandas is using to decide how to order, I would have suspected the indexes to remain sequential for an equal value.

mydf=pd.DataFrame(np.random.randint(1, 6, 20),columns=["stars"])
mydf.sort(['stars'], ascending=False)


     stars
19   5
14   5
1    4
15   4
6    4
7    4
4    3
12   3
18   3
8    2
2    2
9    2
10   2
11   2
13   2
16   2
5    1
3    1
17   1
0    1

推荐答案

Pandas正在使用numpy的快速排序. Quicksort涉及交换项目的位置.它们按照请求的顺序停止(在这种情况下,它不涉及检查索引,因为您不要求检查该列). Quicksort比单纯的排序算法(例如气泡排序)效率更高.请记住-这样会使各个数字更接近其原始顺序,但需要更多步骤来做到这一点.

Pandas is using numpy's quicksort. Quicksort involves swapping positions of the items. It stops once they are in the requested order (which in this case, does not involve checking the indices because you didn't ask for that column to be checked). Quicksort is much more efficient than a naive sort algorithm such as bubble sort, which might be what you have in mind-- it would leave the individual numbers closer to their original order, but require more steps to do so.

这篇关于领带打结时,大 pandas 如何决定排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆