按自定义值列表对 pandas df 列进行排序

Sort pandas df column by a custom list of values

有了这个 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' ,'value_not_present_in_sorter'],\
 'G':[6 ,7 ,60 ,52 ,81]})

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

假设这是自定义列表:

sorter = ['TOT', 'ATL', 'BOS', 'BRK', 'CHA', 'CHH', 'CHI', 'CLE', 'DAL','DEN',\
          'DET', 'GSW', 'HOU', 'IND', 'LAC', 'LAL', 'MEM', 'MIA', 'MIL',\
          'MIN', 'NJN', 'NOH', 'NOK', 'NOP', 'NYK', 'OKC', 'ORL', 'PHI',\
          'PHO', 'POR', 'SAC', 'SAS', 'SEA', 'TOR', 'UTA', 'VAN',\
          'WAS', 'WSB']

我知道这里的答案:sorting by a custom list in pandas

它提供了这个解决方案:

df.Tm = df.Tm.astype("category")
df.Tm.cat.set_categories(sorter, inplace=True)

df.sort_values(["Tm"]) 

但该答案已有 4 年历史,排序器中不存在的列中的值被 替换为 nans(我不想要)。我知道我可以使用 .unique() 并附加到列表的末尾。

所以我的问题是:现在有没有更好的方法通过使用 pandas 新的内置功能来进行自定义排序,如果保留列中的所有值而不用 nans 替换它们,是否有比以下更好的解决方案:

other_values = set(df["TM"].unique()) - set(sorter)
sorter.append(other_values)

您提到的解决方案是一个很好的起点。您可以将 ordered=Trueset_categories 一起使用,以确保您根据需要设置分类排序:

df['Tm'] = df['Tm'].astype('category')
not_in_list = df['Tm'].cat.categories.difference(sorter)
df['Tm'] = df['Tm'].cat.set_categories(np.hstack((sorter, not_in_list)), ordered=True)

df = df.sort_values('Tm')

print(df)

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