0

I've a list of teams as below:

|Team|
|---|
|A|
|B|
|C|

What would be the pandas/python way to create the following table of fixtures:

|Home Team|Away Team|
|---|---|
|A|B|
|A|C|
|B|A|
|B|C|
|C|A|
|C|B|

Each team plays the other team twice (home and away).

Samira Kumar
  • 509
  • 5
  • 14
  • This so called cross join – BENY Jul 29 '19 at 20:40
  • Looks like cross join solved the question. Added one extra step to drop teams playing against themselves. ```all_fixtures = current_teams.assign(key=1).merge(current_teams.assign(key=1), on='key').drop('key', 1) all_fixtures.drop(all_fixtures[all_fixtures['Team_x']==all_fixtures['Team_y']].index,inplace=True) ``` – Samira Kumar Jul 29 '19 at 20:49
  • An alternative to `.merge` could be [`pd.MultiIndex.from_product`](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.MultiIndex.from_product.html) – help-ukraine-now Jul 29 '19 at 21:10
  • `teams = ['A', 'B', 'C']`; `df = pd.MultiIndex.from_product([teams, teams], names=['Home Team', 'Away Team']).to_frame(index=False)`; `df.drop(df.loc[df['Home Team'].eq(df['Away Team'])].index, inplace=True)` – help-ukraine-now Jul 29 '19 at 21:11

0 Answers0