![](/img/trans.png)
[英]how to sort the elements in a list in numerical order and not alphabetical
[英]How to sort table visualization by column in alphabetical + numerical order?
Powerbi
也是microsoft product
,所以我认为对于类似于 Explorer 的排序,它也使用 API StrCmpLogicalW ( ) 进行这种排序(称为“ natural sort order
”)。 一个很好的解释可以在这里找到。
import numpy as np
import pandas as pd
import re
df = pd.DataFrame({
'Subworkload': ['R', 'W10', 'W11', 'W2', 'W3', 'W4', 'W5', 'W6', 'W7', 'W8', 'W9'],
'Iteration': np.zeros(11),
'Parameters': ['Buffer_size=Default_Num_Threads=1' for i in range(11)]
})
def sort_nicely( l ):
""" Sort the given list in the way that humans expect."""
convert = lambda text: int(text) if text.isdigit() else text
alphanum_key = lambda key: [ convert(c) for c in re.split('([0-9]+)', key) ]
l.sort( key=alphanum_key )
return l
val = sort_nicely(list(df['Subworkload']))
print(val)
: Output:
'R', 'W2', 'W3', 'W4', 'W5', 'W6', 'W7', 'W8', 'W9', 'W10', 'W11']
但根据StrCmpLogicalW
output 应该是:
'R', 'W10', 'W11', 'W2', 'W3', 'W4', 'W5', 'W6', 'W7', 'W8', 'W9']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.