在pandas中按自定义列表排序

145

阅读以下内容后: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

我希望能够按照球员、年份和队伍进行排序。球员和年份默认按正常顺序排序对我来说已经可以了。但是,我不想按字母顺序排序队伍,因为我希望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的标识,这意味着它是按照字母顺序排序而不是按照我的自定义列表排序。任何帮助都将不胜感激,我真的想不出怎么解决这个问题。


1
将“DataFrame”添加一个额外列来存储你团队排序索引的数据,是否存在不可避免的理由? - Raman Shah
没有强制要求,只是好奇为什么我的代码不起作用。 - itjcms18
1
在编程中,与value_counts()有些相关:https://dev59.com/ylcP5IYBdhLWcg3wxMw5。 - Skippy le Grand Gourou
9个回答

130
下面的答案是旧答案。它仍然有效。无论如何,另一个非常优雅的解决方案已经被发布在下面,使用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
# You could also do both lines in one just appending the cat.set_categories()
df.Tm = df.Tm.astype("category")
df.Tm = df.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

1
不错!我还没有看到过pandas的类别类型实际应用,它看起来非常有用。 - cd98
1
合并这两行代码,得到类似df.Tm.astype('...').cat.set_cat...的东西,对我来说没用。我不得不像您在此处展示的那样将其打出来。 - raphael
df中不在排序器中的值将被替换为NaN。 - Claudiu Creanga
1
我不明白为什么,但是我必须删除 inplace=True 并重新分配列,才能使其正常工作。 - Fabio Filippi
2
@fabiofili2pi - inplace=True已被弃用,现在正确的语法是df.Tm = f.Tm.cat.set_categories(sorter) - June Skeeter
谢谢,我会适应。 - dmeu

73

以下是一个对数据框执行词典排序的示例。 其思路是基于特定排序创建数字索引。 然后根据索引执行数字排序。 为此,需要向数据框添加一列,然后再将其删除。

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]})

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

# Create the dictionary that defines the order for sorting
sorterIndex = dict(zip(sorter, range(len(sorter))))

# Generate a rank column that will be used to sort
# the dataframe numerically
df['Tm_Rank'] = df['Tm'].map(sorterIndex)

# Here is the result asked with the lexicographic sort
# Result may be hard to analyze, so a second sorting is
# proposed next
## NOTE: 
## Newer versions of pandas use 'sort_values' instead of 'sort'
df.sort_values(['Player', 'Year', 'Tm_Rank'],
        ascending = [True, True, True], inplace = True)
df.drop('Tm_Rank', 1, inplace = True)
print(df)

# Here is an example where 'Tm' is sorted first, that will 
# give the first row of the DataFrame df to contain TOT as 'Tm'
df['Tm_Rank'] = df['Tm'].map(sorterIndex)
## NOTE: 
## Newer versions of pandas use 'sort_values' instead of 'sort'
df.sort_values(['Tm_Rank', 'Player', 'Year'],
        ascending = [True , True, True], inplace = True)
df.drop('Tm_Rank', 1, inplace = True)
print(df)

1
使用map生成排序列会更快,因为它使用了Cython,所以可以这样做:df['Tm_Rank'] = df['Tm'].map(sorterIndex),然后使用此列进行排序,最后删除。 - EdChum
太棒了,谢谢。你知道为什么将升序设置为一个列表不起作用吗?我是不是读错文档了? - itjcms18
这对我有用 - 来自@dmeu的答案在排序列中出现了一些空白,原因不详。谢谢。(另外sort现在称为sort values) - DavidC

53
df1 = df.set_index('Tm')
df1.loc[sorter]

正如 @kstajer 评论中所指出的,在 pandas 1.0.0 之后,请使用 reindex 替代:

df1.reindex(sorter)

1
这是最好的简洁有效的解决方案。 - Fernando Ortega
简洁而有效... +1。 - Mr. Unnormalized Posterior
1
我不常使用 Pandas - 是我自己的问题还是 Pandas 的默认选项经常违背常识?我不得不使用 df.set_index('Tm', drop=False) 来防止删除所选列。 - dariober
3
这应该被接受为答案。 - Iván Sánchez
总的最佳答案。我不得不使用df.reset_index()将列从索引中取出。 - Masih
关键错误: "不再支持将缺失标签的列表传递给.loc或[]。" 请使用df1.reindex(sorter)代替df1.loc[sorter]。这里有更多信息: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#deprecate-loc-reindex-listlike - kstajer

25
自从1.1.0版本,您可以使用“key”属性对值进行排序:
df.sort_values(by="Tm", key=lambda column: column.map(lambda e: sorter.index(e)), inplace=True)

4
太好了,谢谢!我从我的(旧)答案中引用了你的答案。现在这是更好的选择。 - dmeu

21
根据 pandas 1.1.0 文档,现在已经可以使用 key 参数进行排序,就像在 sorted 函数中一样(终于!)。以下是按 Tm 排序的方法。
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)


def tm_sorter(column):
    """Sort function"""
    teams = ['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']
    correspondence = {team: order for order, team in enumerate(teams)}
    return column.map(correspondence)

df.sort_values(by='Tm', key=tm_sorter)

很遗憾,看起来我们只能在按照一列进行排序时使用此功能(带有key的列表是不可接受的)。可以通过groupby绕过此限制。

df.sort_values(['Player', 'Year']) \
  .groupby(['Player', 'Year']) \
  .apply(lambda x: x.sort_values(by='Tm', key=tm_sorter)) \
  .reset_index(drop=True)

如果您知道如何在使用多列的情况下,在 sort_values 中使用 key,请告诉我。


2
请注意,您仍然可以按多列对数据框进行排序(例如df.sort_values(by=['col1', 'col2'], key=mysort)),但是 key 函数一次只会收到一列。我想,如果您的键函数是内联的,如果您想要一列基于与另一列的比较进行排序,可以访问整个数据帧对象。我喜欢您使用 map 的解决方案,但想指出同样可以使用cat = pd.Categorical(column, categories=teams, ordered=True) 然后 return pd.Series(cat) 来完成。 - totalhack

16

这只需几行代码就能完成任务

# Create a dummy df with the required list and the col name to sort on
dummy = pd.Series(sort_list, name = col_name).to_frame()

# Use left merge on the dummy to return a sorted df
sorted_df = pd.merge(dummy, df, on = col_name, how = 'left')

优秀的简洁解决方案 - lys

11

如果需要按照单个自定义列表排序,则设置索引,然后使用DataFrame.loc非常有用。因为loc会为sorter中不在DataFrame中的值创建NaN行,我们首先要找到交集。这可以防止任何不必要的向上转换。任何值不在列表中的行都将被删除。

true_sort = [s for s in sorter if s in df.Tm.unique()]
df = df.set_index('Tm').loc[true_sort].reset_index()

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

起始数据:

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

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

2

针对那些只想按分类列排序的部分解决方案:

您可以使用一个帮助函数,从自定义列表中创建排序顺序映射器。

此示例仅包括来自一列的值,但是通过创建包含所有列中出现的值的自定义顺序列表,可以扩展到包括其他列。自然地,由于必须使用排序字段中的所有可能值构建自定义列表,因此这主要适用于分类排序,并且不适用于连续变量(除非事先知道可能的值)和具有非常高基数的列。

import pandas as pd

# set up a dummy dataframe
df = pd.DataFrame({'a':list('abcde'), 'b':range(5)})

# helper function
def make_sorter(l):
    """
    Create a dict from the list to map to 0..len(l)
    Returns a mapper to map a series to this custom sort order
    """
    sort_order = {k:v for k,v in zip(l, range(len(l)))}
    return lambda s: s.map(lambda x: sort_order[x])

# define a custom sort order
my_order = list('bdeca')

df.sort_values('a', key=make_sorter(my_order))

   a b
1  b 1
3  d 3
4  e 4
2  c 2
0  a 0

使用OP的数据:

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]
})

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

df.sort_values('Tm', key=make_sorter(sorter))

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

0

我的想法是通过索引生成排序数字,然后将排序数字合并到原始数据框中。

import pandas as pd

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]})

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

x = pd.DataFrame({'Tm': sorter})
x.index = x.index.set_names('number')
x = x.reset_index()

df = pd.merge(df, x, how='left', on='Tm')

df.sort_values(['Player', 'Year', 'number'], \
        ascending = [True, True, True], inplace = True)
df.drop('number', 1, inplace = True)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接