Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 无法按值降序排序数据帧,然后按字母升序排序_Python 3.x_Pandas_Sorting_Alphabetical Sort - Fatal编程技术网

Python 3.x 无法按值降序排序数据帧,然后按字母升序排序

Python 3.x 无法按值降序排序数据帧,然后按字母升序排序,python-3.x,pandas,sorting,alphabetical-sort,Python 3.x,Pandas,Sorting,Alphabetical Sort,我的数据帧的一个片段df是这样的,所以您可以复制它 data ={'feature_name': ['nite', 'thank', 'ok', 'havent', 'done', 'beverage', 'yup', 'lei','thanx', 'okie', '146tf150p', 'home', 'too', 'anytime', 'where', '645', 'er', 'tick', 'blank'], 'values':[1.0, 1.0, 1.0, 1.0, 1

我的数据帧的一个片段df是这样的,所以您可以复制它

data ={'feature_name': ['nite', 'thank', 'ok', 'havent', 'done', 'beverage', 'yup', 'lei','thanx', 'okie', '146tf150p', 'home', 'too', 'anytime',
       'where', '645', 'er', 'tick', 'blank'], 'values':[1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.98, 0.98] }
df = pd.DataFrame(data)
df.set_index('feature_name',inplace=True)
dfs=df.sort_index(ascending=True).sort_values(by = ['values'], ascending=False)
dfs
我的输出是这个

            values
feature_name    
146tf150p   1.00
645         1.00
where       1.00
too         1.00
thanx       1.00
thank       1.00
okie        1.00
ok          1.00
nite        1.00
lei         1.00
home        1.00
havent      1.00
er          1.00
done        1.00
beverage    1.00
anytime     1.00
yup         1.00
blank       0.98
tick        0.98
我不太明白为什么不是这样?它确实应该起作用,但并没有如预期的那样起作用


146tf150p   1.00
645         1.00
anytime     1.00
beverage    1.00
done        1.00
er          1.00
haven't     1.00
home        1.00
...
如何解决此问题?

摆脱set\u索引,对值和功能名称使用sort\u值:

print (df.sort_values(by = ['values',"feature_name"], ascending=(False, True)))

   feature_name  values
10    146tf150p    1.00
15          645    1.00
13      anytime    1.00
5      beverage    1.00
4          done    1.00
16           er    1.00
3        havent    1.00
11         home    1.00
7           lei    1.00
0          nite    1.00
2            ok    1.00
9          okie    1.00
1         thank    1.00
8         thanx    1.00
12          too    1.00
14        where    1.00
6           yup    1.00
18        blank    0.98
17         tick    0.98

因为你做了一个.sort_valuesby=['values'],升序=False,这会在以后调整排序为什么更简单的东西是可以的?数据={'feature_name':[newt,tiger,otter,仓鼠,deer,獾,'tortoise','boar','values':[1.00,1.00,1.00,1.00,1.00,1.00,0.98,0.98]}df=pd.DataFramedata df.set_index'feature_name',in place=True dfsorted=df.sorted\u values by=['values',feature_name']升序=false我不明白你的意思。。也许你可以把你的问题修改得更清楚1.我想你想要的是1。不要将索引设置为要素名称2。使用df.sort_valuesby=['values',feature_name],升序=False,True。你能把你的答案作为答案发布,而不是放在评论部分,这样我就可以投赞成票了吗?