Would df.sort_values('A', kind = 'mergesort').sort_index(kind = 'mergesort') be a stable and valid way to sort by index and column?

huangapple go评论61阅读模式
英文:

Would df.sort_values('A', kind = 'mergesort').sort_index(kind = 'mergesort') be a stable and valid way to sort by index and column?

问题

可以使用以下代码来对Pandas DataFrame进行排序以满足您的要求:

df.sort_values('A', kind='mergesort').sort_index(kind='mergesort')

这将首先按照列'A'进行排序,使用稳定的归并排序算法('mergesort'),然后按索引列进行排序,同样使用稳定的归并排序算法。这个操作将在稳定的方式下对DataFrame进行排序,确保索引列严格有序,而对于重复的索引,它们将按照列'A'的值进行排序。

在这种方法下,.sort_index()不会破坏先前的.sort_values()操作,而是继续在先前的排序基础上对索引进行排序。这将确保'DataFrame'在稳定的方式下得到排序,以满足您的要求。

英文:

I have a Pandas dataframe equivalent to:

              'A' 'B'
'i1' 'i2' 'i3'
 1    2   4    3   0
 1    1   2    3   3
 1    1   2    1   0
 1    2   4    0   9
 1    1   2    2   6
 2    1   1    1   8

where ix are index columns and 'A', and 'B' are normal columns. I want to make sure that the indexes are strictly ordered and, when indexes are duplicated, then it is ordered by column 'A'

              'A' 'B'
'i1' 'i2' 'i3'
 1    1   2    1   0
 1    1   2    2   6
 1    1   2    3   3
 1    2   4    0   9
 1    2   4    3   0
 2    1   1    1   8
 

Would df.sort_values('A', kind = 'mergesort').sort_index(kind = 'mergesort') do it? And if so, would do it in a stable way? or could the .sort_index() operation disrupt the previous .sort_values() operation in such a way that, for the duplicated indexes, the values of 'A' are no longer ordered?

答案1

得分: 1

当您按多个键进行排序时,只能保证最后一个键被排序。其他键将在先前的组内进行排序。最后,对于稳定排序(如合并排序),非键列将保持在原始顺序中排序。

回答您的问题,是的,在出现重复键的情况下,您的方法将保持原始顺序。

英文:

When you sort by multiple keys, only the last one is guaranteed to be sorted. The others will be sorted within the previous groups. Finally, the non-key columns will remain sorted in the original order in case of a stable sort such as the mergesort.

To answer your question, yes, your method will maintain the original order in case of duplicated keys.

huangapple
  • 本文由 发表于 2023年2月18日 00:18:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/75486770.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定