python - 按 Pandas 中的自定义列表排序

标签 python sorting pandas

通读后:http://pandas.pydata.org/pandas-docs/version/0.13.1/generated/pandas.DataFrame.sort.html

我似乎仍然无法弄清楚如何按自定义列表对列进行排序。显然,默认排序是按字母顺序排列的。我举个例子。这是我的(非常精简的)数据框:

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

我希望能够按 Player、Year 和 Tm 排序。按玩家和年份的默认排序对我来说很好,按正常顺序。但是,我不希望团队按字母顺序排序 b/c 我希望 TOT 始终位于顶部。

这是我创建的列表:

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']

阅读完上面的链接后,我认为这会起作用,但它没有:

df.sort(['Player', 'Year', 'Tm'], ascending = [True, True, sorter])

它仍然在顶部有 ATL,这意味着它按字母顺序排序,而不是根据我的自定义列表。任何帮助将不胜感激,我只是想不通。

最佳答案

以下答案是旧答案。它仍然有效。无论如何,已经发布了另一个非常优雅的解决方案(如下),使用 key 参数。


我刚刚发现使用 pandas 15.1 可以使用分类系列 (https://pandas.pydata.org/docs/user_guide/categorical.html)

至于您的示例,让我们定义相同的数据框和排序器:

import pandas as pd

data = {
    '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]
}

# Create DataFrame
df = pd.DataFrame(data)

# Define the sorter
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']

使用数据框和排序器,这是一个类别顺序,我们可以在 pandas 15.1 中执行以下操作:

# Convert Tm-column to category and in set the sorter as categories hierarchy
# Youc could also do both lines in one just appending the cat.set_categories()
df.Tm = df.Tm.astype("category")
df.Tm = f.Tm.cat.set_categories(sorter)

print(df.Tm)
Out[48]: 
0    CHH
1    VAN
2    TOT
3    OKC
4    DAL
Name: Tm, dtype: category
Categories (38, object): [TOT < ATL < BOS < BRK ... UTA < VAN < WAS < WSB]

df.sort_values(["Tm"])  ## 'sort' changed to 'sort_values'
Out[49]: 
   Age   G           Player   Tm  Year     id
2   22  60      Ratko Varda  TOT  2001  13950
0   27   6    Cedric Hunter  CHH  1991   2967
4   31  81  Adrian Caldwell  DAL  1997   6169
3   34  52       Ryan Bowen  OKC  2009   6141
1   25   7    Maurice Baker  VAN  2004   5335

关于python - 按 Pandas 中的自定义列表排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23482668/

相关文章:

python - Django:密码如何存储在 Django auth_user 表中

python - PyQt4 - 使用 QItemDelegate 在 QListView 中显示小部件

python - 如何在多级数据帧上正确使用 .loc?

python - Pandas 将行中上一个值和下一个值的平均值归咎于 Null

python - 如何使用 wxPython 创建具有 alpha channel 透明度的窗口?

python - 从另一个线程或进程更新 Gtk.ProgressBar

javascript - 基于Javascript内部数组中的第二个值对嵌套数组进行排序

python - 类型错误 : Can't convert 'int' object to str implicitly/Sorting Array issue

arrays - 通过从另一个数组排序来对 Swift 数组进行排序

python - pandas 操作期间的进度指示器