熊猫-根据组和条件对行进行重新排序

8位博尔赫斯

我有3个数据框,每个数据框按位置(前进,后卫和守门员)列出了一个球员列表:

FWD_df = pd.DataFrame({'name':['Keno','Pepê','Rhuan','Léo Natel','Pedro Raul','Wesley'],
                        'team':['Atlético-MG','Grêmio','Botafogo','Corinthians','Botafogo','Palmeiras'],
                        'adversary':['Fluminense','Botafogo','Grêmio','Athlético-PR','Grêmio','Coritiba'], 
                         'position':['FWD', 'FWD', 'FWD', 'FWD', 'FWD', 'FWD'],
                         'open_price':[15.75, 14.05, 3.51, 5.83, 5.92, 4.25]})


MID_df = pd.DataFrame({'apelido':['Everton Ribeiro','Thiago Galhardo','Thiago Neves','Mateus Vital','Dodi','Zé Gabriel'],
                       'clube':['Flamengo','Internacional','Sport','Corinthians','Fluminense', 'Internacional'],
                       'adversário':['Bragantino','Sport','Internacional','Athlético-PR','Atlético-MG','Sport'],  
                       'posicao':['MID', 'MID', 'MID','MID', 'MID', 'MID'],
                       'preco_open':[10.82, 6.46, 4.96, 8.20, 5.29, 7.88]}).sort_values('preco_open', ascending=False).reset_index(drop=True).copy()


WING_df = pd.DataFrame({'apelido':['Renê','Abner Vinícius','Marcos Rocha','Victor Luis','Fagner','Ramon'],
                       'clube':['Flamengo','Athlético-PR','Palmeiras','Botafogo','Corinthians', 'Flamengo'],
                       'adversário':['Bragantino','Corinthians','Coritiba','Grêmio','Athlético-PR','Bragantino'],  
                       'posicao':['WING', 'WING', 'WING','WING', 'WING', 'WING'],
                       'preco_open':[10.82, 6.46, 4.96, 8.20, 5.29, 7.88]}).sort_values('preco_open', ascending=False).reset_index(drop=True).copy()

DEF_df = pd.DataFrame({'name':['Pedro Geromel','Felipe Melo','Pedro Henrique','Réver','Thiago Heleno','Lucas Veríssimo'],
                       'team':['Grêmio','Palmeiras','Athlético-PR','Atlético-MG','Athlético-PR', 'Santos'],
                       'adversary':['Botafogo','Coritiba','Corinthians','Fluminense','Corinthians','Atlético-GO'],  
                       'position':['DEF', 'DEF', 'DEF','DEF', 'DEF', 'DEF'],
                       'open_price':[10.82, 6.46, 4.96, 8.20, 5.29, 7.88]})

GKP_df = pd.DataFrame({'name':['Jandrei','Jean','Muriel','Diego Cavalieri','Marcelo Lomba','Luan Polli'],
                       'team':['Athlético-PR','Atlético-GO','Fluminense','Botafogo','Internacional', 'Sport'],
                       'adversary':['Corinthians','Santos','Atlético-MG','Grêmio','Sport','Internacional'], 
                       'position':['GKP', 'GKP', 'GKP','GKP', 'GKP', 'GKP'],
                       'open_price':[8.73, 8.88, 5.66, 5.70, 10.62, 4.00]})

然后,我为那些选定的玩家创建一个池,如下所示:

dfs = [FWD_df, DEF_df, GKP_df]

pool = pd.concat(dfs)

print (pool)

打印:

              name           team      adversary position  open_price
0             Keno    Atlético-MG     Fluminense      FWD       15.75
1             Pepê         Grêmio       Botafogo      FWD       14.05
2            Rhuan       Botafogo         Grêmio      FWD        3.51
3        Léo Natel    Corinthians   Athlético-PR      FWD        5.83
4       Pedro Raul       Botafogo         Grêmio      FWD        5.92
5           Wesley      Palmeiras       Coritiba      FWD        4.25
0    Pedro Geromel         Grêmio       Botafogo      DEF       10.82
1      Felipe Melo      Palmeiras       Coritiba      DEF        6.46
2   Pedro Henrique   Athlético-PR    Corinthians      DEF        4.96
3            Réver    Atlético-MG     Fluminense      DEF        8.20
4    Thiago Heleno   Athlético-PR    Corinthians      DEF        5.29
5  Lucas Veríssimo         Santos    Atlético-GO      DEF        7.88
0          Jandrei   Athlético-PR    Corinthians      GKP        8.73
1             Jean    Atlético-GO         Santos      GKP        8.88
2           Muriel     Fluminense    Atlético-MG      GKP        5.66
3  Diego Cavalieri       Botafogo         Grêmio      GKP        5.70
4    Marcelo Lomba  Internacional          Sport      GKP       10.62
5       Luan Polli          Sport  Internacional      GKP        4.00

规则

问题是,从前三名开始,我无法面对一名前两名防守者一名守门员,也就是说,如果他是他的下一个“对手”,那么:

             name           team          adversary position  open_price
2            Rhuan       Botafogo         Grêmio      FWD        3.51
...
0    Pedro Geromel         Grêmio       Botafogo      DEF       10.82

发生这种情况时,我需要将最有价值的参与者(最高的“ open_price”)保留在其行位置,并将最便宜的参与者移至其最后分组的行索引。

在上述情况下,防御者的费用更高,因此他留在原地,我们将FWD向下移动到其位置的最后一个索引:

             name           team       adversary position  open_price
0             Keno    Atlético-MG     Fluminense      FWD       15.75
1             Pepê         Grêmio       Botafogo      FWD       14.05
2        Léo Natel    Corinthians   Athlético-PR      FWD        5.83
3       Pedro Raul       Botafogo         Grêmio      FWD        5.92
4           Wesley      Palmeiras       Coritiba      FWD        4.25
5            Rhuan       Botafogo         Grêmio      FWD        3.51 <---- was index 2, now 5
0    Pedro Geromel         Grêmio       Botafogo      DEF       10.82
1      Felipe Melo      Palmeiras       Coritiba      DEF        6.46
2   Pedro Henrique   Athlético-PR    Corinthians      DEF        4.96
3            Réver    Atlético-MG     Fluminense      DEF        8.20
4    Thiago Heleno   Athlético-PR    Corinthians      DEF        5.29
5  Lucas Veríssimo         Santos    Atlético-GO      DEF        7.88
0          Jandrei   Athlético-PR    Corinthians      GKP        8.73
1             Jean    Atlético-GO         Santos      GKP        8.88
2           Muriel     Fluminense    Atlético-MG      GKP        5.66
3  Diego Cavalieri       Botafogo         Grêmio      GKP        5.70
4    Marcelo Lomba  Internacional          Sport      GKP       10.62
5       Luan Polli          Sport  Internacional      GKP        4.00

但是通过这样做,现在我们将在前三名中拥有一个前锋,它将面对一个守门员,这也是规则所禁止的:

             name            team      adversary position  open_price
2        Léo Natel    Corinthians   Athlético-PR      FWD        5.83
...
0          Jandrei   Athlético-PR    Corinthians      GKP        8.73

而且由于守门员比前锋花费更多,他会留在原地,我们将前锋下移,如下所示:

             name           team       adversary position  open_price
0             Keno    Atlético-MG     Fluminense      FWD       15.75
1             Pepê         Grêmio       Botafogo      FWD       14.05
2       Pedro Raul       Botafogo         Grêmio      FWD        5.92
3           Wesley      Palmeiras       Coritiba      FWD        4.25
4            Rhuan       Botafogo         Grêmio      FWD        3.51 <---- was index 5, now 4
5        Léo Natel    Corinthians   Athlético-PR      FWD        5.83 <---- was index 2, now 5
0    Pedro Geromel         Grêmio       Botafogo      DEF       10.82
1      Felipe Melo      Palmeiras       Coritiba      DEF        6.46
2   Pedro Henrique   Athlético-PR    Corinthians      DEF        4.96
3            Réver    Atlético-MG     Fluminense      DEF        8.20
4    Thiago Heleno   Athlético-PR    Corinthians      DEF        5.29
5  Lucas Veríssimo         Santos    Atlético-GO      DEF        7.88
0          Jandrei   Athlético-PR    Corinthians      GKP        8.73
1             Jean    Atlético-GO         Santos      GKP        8.88
2           Muriel     Fluminense    Atlético-MG      GKP        5.66
3  Diego Cavalieri       Botafogo         Grêmio      GKP        5.70
4    Marcelo Lomba  Internacional          Sport      GKP       10.62
5       Luan Polli          Sport  Internacional      GKP        4.00

再一次,我们将不得不再次这样做:

             name           team       adversary position  open_price
0             Keno    Atlético-MG     Fluminense      FWD       15.75
1             Pepê         Grêmio       Botafogo      FWD       14.05
2           Wesley      Palmeiras       Coritiba      FWD        4.25 <---- was index 3, now 2
3            Rhuan       Botafogo         Grêmio      FWD        3.51 <---- was index 4, now 3
4        Léo Natel    Corinthians   Athlético-PR      FWD        5.83 <---- was index 5, now 4
5       Pedro Raul       Botafogo         Grêmio      FWD        5.92 <---- was index 3, now 5
0    Pedro Geromel         Grêmio       Botafogo      DEF       10.82
1      Felipe Melo      Palmeiras       Coritiba      DEF        6.46
2   Pedro Henrique   Athlético-PR    Corinthians      DEF        4.96
3            Réver    Atlético-MG     Fluminense      DEF        8.20
4    Thiago Heleno   Athlético-PR    Corinthians      DEF        5.29
5  Lucas Veríssimo         Santos    Atlético-GO      DEF        7.88
0          Jandrei   Athlético-PR    Corinthians      GKP        8.73
1             Jean    Atlético-GO         Santos      GKP        8.88
2           Muriel     Fluminense    Atlético-MG      GKP        5.66
3  Diego Cavalieri       Botafogo         Grêmio      GKP        5.70
4    Marcelo Lomba  Internacional          Sport      GKP       10.62
5       Luan Polli          Sport  Internacional      GKP        4.00

直到我们结束上面的数据框,即最终池。


我该如何对行进行条件重新排序?有人可以在这里指出正确的方向吗?

拉斐尔克

您有一个问题,需要根据约束条件找到一个平衡点。

我在这里提出一个不使用的解决方案pandas,而是纯Python列表,因为熊猫的开销只会减慢解决方案的速度。

首先,显然存在无限循环的可能性。因此,您必须跟踪过去的排列配置,以便知道何时绕圈并开始循环。

我创建了一个自定义错误InfeasableError(出于可读性),每当发生这种情况时都会引发此错误其余代码应该很简单。

Setup

class InfeasableError(Exception):
  pass

# These are the positions on the lists for team, adv and prices.
TEAM_POS = 2
ADVERSARY_POS = 3
PRICE_POS = 5

fwd = FWD_df.reset_index().values.tolist()
dfs = DEF_df.reset_index().values.tolist()
gkp = GKP_df.reset_index().values.tolist()
mid = MID_df.reset_index().values.tolist()
wig = WING_df.reset_index().values.tolist()

现在,均衡发现函数为:

def find_equilibrium(fwd, dfs, gkp, mid, wig, configs):
''' Finds an equilibrium for forward, defense and goalkeeper players.
'''

  initial_config = get_config(fwd, dfs, gkp) # Initial configuration

  # Below is the logic for FWD-DEF equilibria.

  for i, fwd_player in enumerate(fwd[:3]):
    for j, def_player in enumerate(dfs[:2]):
      if fwd_player[ADVERSARY_POS] == def_player[TEAM_POS]:
        if fwd_player[PRICE_POS] < def_player[PRICE_POS]:
          del fwd[i]
          fwd.append(fwd_player)
        else:
          del dfs[j]
          dfs.append(def_player)

  # Below is the logic for FWD-GKP equilibria.

  gkp_player = gkp[0]
  for i, fwd_player in enumerate(fwd[:3]):
    if fwd_player[ADVERSARY_POS] == gkp_player[TEAM_POS]:
      if fwd_player[PRICE_POS] < gkp_player[PRICE_POS]:
        del fwd[i]
        fwd.append(fwd_player)
      else:
        del gkp[0]
        gkp.append(gkp_player)
  
  final_config = get_config(fwd, dfs, gkp)
  if initial_config == final_config:
    # This means nothing changed. Equilibrium found!
    return [*fwd, *dfs, *gkp]

  if final_config in configs: 
    raise InfeasableError("There's no solution for an equilibrium.")

  configs.append(final_config)
  return find_equilibrium(fwd, dfs, gkp, mid, wig, configs)

def get_config(fwd, dfs, gkp):
  ''' Returns an ordered configuration of integers. 
      Each integer represents a unique player, and each sequence represents
      a specific configuration of how these players are arranged.
  '''
  return (fwd[0][0], fwd[1][0], fwd[2][0], dfs[0][0], dfs[1][0], gkp[0][0])

solution = find_equilibrium(fwd, dfs, gkp, mid, wig, [])

如果需要,该解决方案可以直接导入到熊猫中:

>>> pd.DataFrame(solution)

    0                1              2              3    4      5
0   0             Keno    Atlético-MG     Fluminense  FWD  15.75
1   1             Pepê         Grêmio       Botafogo  FWD  14.05
2   5           Wesley      Palmeiras       Coritiba  FWD   4.25
3   2            Rhuan       Botafogo         Grêmio  FWD   3.51
4   3        Léo Natel    Corinthians   Athlético-PR  FWD   5.83
5   4       Pedro Raul       Botafogo         Grêmio  FWD   5.92
6   0    Pedro Geromel         Grêmio       Botafogo  DEF  10.82
7   1      Felipe Melo      Palmeiras       Coritiba  DEF   6.46
8   2   Pedro Henrique   Athlético-PR    Corinthians  DEF   4.96
9   3            Réver    Atlético-MG     Fluminense  DEF   8.20
10  4    Thiago Heleno   Athlético-PR    Corinthians  DEF   5.29
11  5  Lucas Veríssimo         Santos    Atlético-GO  DEF   7.88
12  0          Jandrei   Athlético-PR    Corinthians  GKP   8.73
13  1             Jean    Atlético-GO         Santos  GKP   8.88
14  2           Muriel     Fluminense    Atlético-MG  GKP   5.66
15  3  Diego Cavalieri       Botafogo         Grêmio  GKP   5.70
16  4    Marcelo Lomba  Internacional          Sport  GKP  10.62
17  5       Luan Polli          Sport  Internacional  GKP   4.00

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

根据熊猫中的多个条件对行进行分组和过滤

熊猫:根据列的值对行进行排序

使用多个条件对熊猫进行排序和重新分组

熊猫分组,然后根据组进行排序

如何使用熊猫对组中的行进行排序(降序排列)

如何仅对熊猫组中的某些行进行排序?

熊猫根据其他数据框的条件对行进行划分

根据给定条件对熊猫中的行进行分组

根据条件删除熊猫组

熊猫对所有行进行排序

熊猫按其内容的任意条件对古比组进行排序

熊猫按条件对多行进行分组

如何根据时间条件进行累加-对熊猫重新采样?

如何根据条件对熊猫数据框的行值进行排序?

如何在给定组的适当位置对熊猫中的行进行排序?

对熊猫行进行排名和子排名

根据字符串值列对熊猫数据框行进行排序

根据字符串值的类型对熊猫中的行进行排序

根据值和索引对熊猫系列进行排序

熊猫groupby对每个组值进行排序,并根据每个组的最大值对数据框组进行排序

根据条件丢弃整个熊猫组

根据条件忽略熊猫groupby中的组

大熊猫:对分组数据框中的行进行排序和删除

如何根据行中的特定值和熊猫中的另一列对行进行分组?

根据指定的值对上方和下方的熊猫行进行分组

根据列表对熊猫数据框进行排序

熊猫:在组内使用条件进行迭代

如何对熊猫数据框的一行进行排序

熊猫基于列表对行进行自定义排序