pandas 如何按字符串索引上的自定义顺序对熊猫数据框进行排序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/50012525/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 05:30:19  来源:igfitidea点击:

How to sort pandas dataframe by custom order on string index

pythonpandassortingindexingcategorical-data

提问by scamander

I have the following data frame:

我有以下数据框:

import pandas as pd

# Create DataFrame
df = pd.DataFrame(
{'id':[2967, 5335, 13950, 6141, 6169],\
 'Player': ['Cedric Hunter', 'Maurice Baker' ,\
            'Ratko Varda' ,'Ryan Bowen' ,'Adrian Caldwell'],\
 'Year': [1991 ,2004 ,2001 ,2009 ,1997],\
 'Age': [27 ,25 ,22 ,34 ,31],\
 'Tm':['CHH' ,'VAN' ,'TOT' ,'OKC' ,'DAL'],\
 'G':[6 ,7 ,60 ,52 ,81]})


df.set_index('Player', inplace=True)

It shows:

表明:

Out[128]:

                 Age   G   Tm  Year     id
Player
Cedric Hunter     27   6  CHH  1991   2967
Maurice Baker     25   7  VAN  2004   5335
Ratko Varda       22  60  TOT  2001  13950
Ryan Bowen        34  52  OKC  2009   6141
Adrian Caldwell   31  81  DAL  1997   6169

What I want to do is to sort the 'Player' index in the arbitrary order according to this list (NOTE: not alphabetical order):

我想要做的是根据此列表以任意顺序对“播放器”索引进行排序(注意:不是按字母顺序排列):

reorderlist = [ 'Maurice Baker', 'Adrian Caldwell','Ratko Varda' ,'Ryan Bowen' ,'Cedric Hunter']

How can I do that?

我怎样才能做到这一点?

回答by YOBEN_S

Just reindex

只是 reindex

df.reindex(reorderlist)
Out[89]: 
                 Age   G   Tm  Year     id
Player                                    
Maurice Baker     25   7  VAN  2004   5335
Adrian Caldwell   31  81  DAL  1997   6169
Ratko Varda       22  60  TOT  2001  13950
Ryan Bowen        34  52  OKC  2009   6141
Cedric Hunter     27   6  CHH  1991   2967

回答by smci

To get a custom sort-order on your list of strings, declare it as a categorical and manually specify that order in a sort:

要在字符串列表中获得自定义排序顺序,请将其声明为分类顺序并在排序中手动指定该顺序:

player_order = pd.Categorical([ 'Maurice Baker', 'Adrian Caldwell','Ratko Varda' ,'Ryan Bowen' ,'Cedric Hunter'],
              ordered=True)

This is since pandas does not yet allow Categoricals as indices: df.set_index(keys=player_order, inplace=True)TypeError: unhashable type: 'Categorical'

这是因为 Pandas 还不允许 Categoricals 作为索引: df.set_index(keys=player_order, inplace=True)TypeError: unhashable type: 'Categorical'

So you'll want to do a manual custom sort using df.sort_index(level=player_order)

所以你会想要使用手动自定义排序 df.sort_index(level=player_order)