I put some data together for the 2015 FIFA Women's World Cup:
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()
Now I would like to generate a new dataframe that contains the 6 possible pairings or matches within each group
from df
, in a format like:
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
...
What is a concise and clean way to do this? I can do a bunch of loops through each group
and team
, but I feel like there should be a cleaner vectorized way to do this with pandas
and the split-apply-combine paradigm.
EDIT: I also welcome any R answers, think it'd be interesting to compare between the R and Pandas ways here. Added the r
tag.
Here's the data in R form, as requested in Comments:
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)