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

标签 python pandas sorting series categorical-data

有了这个 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 new 进行自定义排序,如果保留列中的所有值而不用 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

关于python - 按自定义值列表对 pandas df 列进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54202243/

相关文章:

Python:按条件删除重复项

python - 如何计算数据框中的移动平均值?

python - 按元组列表过滤 DataFrame 的行

python - Elasticsearch-py 使用脚本更新 API

python - 使用自定义颜色渐变填充两条线之间的区域

sorting - Pandas DataFrame 排序 : Want to sum and sort, 但保留列名

algorithm - 为什么序列 1,4,13,40,121... 在插入排序时比 1,2,4,8,16... 更有效?

python - 在python中使用sftp从远程机器复制文件

python - 如何在Python设备中获取event.post(时钟,数据,持续时间)的时钟参数?

python - 为什么排序列表检测在这种情况下不起作用?