2

我为 2015 年 FIFA 女足世界杯整理了一些数据:

import pandas as pd

df = pd.DataFrame({
    'team':['Germany','USA','France','Japan','Sweden','England','Brazil','Canada','Australia','Norway','Netherlands','Spain',
       'China','New Zealand','South Korea','Switzerland','Mexico','Colombia','Thailand','Nigeria','Ecuador','Ivory Coast','Cameroon','Costa Rica'],
    'group':['B','D','F','C','D','F','E','A','D','B','A','E','A','A','E','C','F','F','B','D','C','B','C','E'],
    'fifascore':[2168,2158,2103,2066,2008,2001,1984,1969,1968,1933,1919,1867,1847,1832,1830,1813,1748,1692,1651,1633,1485,1373,1455,1589],
    'ftescore':[95.6,95.4,92.4,92.7,91.6,89.6,92.2,90.1,88.7,88.7,86.2,84.7,85.2,82.5,84.3,83.7,81.1,78.0,68.0,85.7,63.3,75.6,79.3,72.8]
    })

df.groupby(['group', 'team']).mean()

输出

现在我想生成一个新的数据框,其中包含每个groupfrom中的 6 个可能的配对或匹配df,格式如下:

group    team1        team2
A        Canada       China
A        Canada       Netherlands
A        Canada       New Zealand
A        China        Netherlands
A        China        New Zealand
A        Netherlands  New Zealand
B        Germany      Ivory Coast
B        Germany      Norway
...     

什么是简洁干净的方法来做到这一点?group我可以通过每个and做一堆循环team,但我觉得应该有一个更干净的矢量化方式来做到这一点,pandas以及拆分-应用-组合范式。

编辑:我也欢迎任何 R 答案,认为在这里比较 R 和 Pandas 方式会很有趣。添加了r标签。

根据评论中的要求,这是 R 形式的数据:

team <- c('Germany','USA','France','Japan','Sweden','England','Brazil','Canada','Australia','Norway','Netherlands','Spain',
      'China','New Zealand','South Korea','Switzerland','Mexico','Colombia','Thailand','Nigeria','Ecuador','Ivory Coast','Cameroon','Costa Rica')
group <- c('B','D','F','C','D','F','E','A','D','B','A','E','A','A','E','C','F','F','B','D','C','B','C','E')
fifascore <- c(2168,2158,2103,2066,2008,2001,1984,1969,1968,1933,1919,1867,1847,1832,1830,1813,1748,1692,1651,1633,1485,1373,1455,1589)
ftescore <- c(95.6,95.4,92.4,92.7,91.6,89.6,92.2,90.1,88.7,88.7,86.2,84.7,85.2,82.5,84.3,83.7,81.1,78.0,68.0,85.7,63.3,75.6,79.3,72.8)

df <- data.frame(team, group, fifascore, ftescore)
4

2 回答 2

3

这是两行解决方案:

import itertools

for grpname,grpteams in df.groupby('group')['team']:
    # No need to use grpteams.tolist() to convert from pandas Series to Python list
    print list(itertools.combinations(grpteams, 2))

[('Canada', 'Netherlands'), ('Canada', 'China'), ('Canada', 'New Zealand'), ('Netherlands', 'China'), ('Netherlands', 'New Zealand'), ('China', 'New Zealand')]
[('Germany', 'Norway'), ('Germany', 'Thailand'), ('Germany', 'Ivory Coast'), ('Norway', 'Thailand'), ('Norway', 'Ivory Coast'), ('Thailand', 'Ivory Coast')]
[('Japan', 'Switzerland'), ('Japan', 'Ecuador'), ('Japan', 'Cameroon'), ('Switzerland', 'Ecuador'), ('Switzerland', 'Cameroon'), ('Ecuador', 'Cameroon')]
[('USA', 'Sweden'), ('USA', 'Australia'), ('USA', 'Nigeria'), ('Sweden', 'Australia'), ('Sweden', 'Nigeria'), ('Australia', 'Nigeria')]
[('Brazil', 'Spain'), ('Brazil', 'South Korea'), ('Brazil', 'Costa Rica'), ('Spain', 'South Korea'), ('Spain', 'Costa Rica'), ('South Korea', 'Costa Rica')]
[('France', 'England'), ('France', 'Mexico'), ('France', 'Colombia'), ('England', 'Mexico'), ('England', 'Colombia'), ('Mexico', 'Colombia')]

解释:

首先,我们使用 获取每个组内的团队df.groupby('group')列表,遍历该列表并访问其“团队”系列,以获取每个组内 4 个团队的列表:

for grpname,grpteams in df.groupby('group')['team']:
    teamlist = grpteams.tolist()
... 
['Canada', 'Netherlands', 'China', 'New Zealand']
['Germany', 'Norway', 'Thailand', 'Ivory Coast']
['Japan', 'Switzerland', 'Ecuador', 'Cameroon']
['USA', 'Sweden', 'Australia', 'Nigeria']
['Brazil', 'Spain', 'South Korea', 'Costa Rica']
['France', 'England', 'Mexico', 'Colombia']

然后我们生成团队元组的全部播放列表。David Arenburg 的帖子提醒我使用itertools.combinations(..., 2). 但是我们可以使用生成器或嵌套的 for 循环:

def all_play_all(teams):
  for team1 in teams:
    for team2 in teams:
      if team1 < team2: # [Note] We don't need to generate indices then index into teamlist, just use direct string comparison
        yield (team1,team2)

>>> [match for match in all_play_all(grpteams)]
[('France', 'Mexico'), ('England', 'France'), ('England', 'Mexico'), ('Colombia', 'France'), ('Colombia', 'England'), ('Colombia', 'Mexico')]

请注意,我们采取了一种捷径,首先生成所有可能的索引元组,然后使用这些元组索引到团队列表中:

>>> T = len(teamlist) + 1
>>> [(i,j) for i in range(T) for j in range(T) if i<j]
[(0, 1), (0, 2), (0, 3), (1, 2), (1, 3), (2, 3)]

(注意:如果我们使用直接比较队名的方法,它会产生轻微的副作用,即(按字母顺序)组名(它们最初是按种子排序,而不是按字母顺序),例如 'China' < '荷兰',因此他们的配对将显示为 ('Netherlands','China') 而不是 ('China',Netherlands'))

于 2015-06-02T21:23:23.093 回答
3

使用 R,这是一个可能的data.table解决方案,在 GitHub 上使用它的开发版本

#### To install development version
## library(devtools)
## install_github("Rdatatable/data.table", build_vignettes = FALSE)

library(data.table) ## v >= 1.9.5
setDT(df)[, transpose(combn(team, 2L, simplify = FALSE)), keyby = group]
#    group          V1          V2
# 1:     A      Canada Netherlands
# 2:     A      Canada       China
# 3:     A      Canada New Zealand
# 4:     A Netherlands       China
# 5:     A Netherlands New Zealand
# 6:     A       China New Zealand
# 7:     B     Germany      Norway
# 8:     B     Germany    Thailand
...
于 2015-06-02T21:34:35.593 回答