1

unfortunately I'm having trouble creating a correct display of mianowiecie values. I have such a DF:

Group Match Team
A 1 A1
A 1 A2
A 2 A3
A 2 A4

I have this code:

for group in set(world_cup['Group']):
    print('___Starting group {}:___'.format(group))
    for home, away in combinations(world_cup.query('Group == "{}"'.format(group)).index, 2):
      #conditions....

I added a third loop so that instead of selecting group it selects game. However, I do not get the expected result because it shows all the matches in one group first and then the same matches in other groups. I would like to get something like this:

___Starting group A:__
A1-A2
A3-A4

Now I get something like this

___Starting group A:__
A1-A2
A1-A3
A1-A4
A2-A3
A2-A4
A3-A4

Thank everyone for help.

Kon Wes
  • 19
  • 3

1 Answers1

0

I hope I've understood you correctly. You can .groupby() and then .agg the values:

out = df.groupby(["Group", "Match"]).agg("-".join)
print(out)

Prints:

              Team
Group Match       
A     1      A1-A2
      2      A3-A4

out = df.groupby(["Group", "Match"]).agg("-".join)

tmp = {}
for idx, row in out.iterrows():
    tmp.setdefault(idx[0], []).append(row["Team"])

for k, v in tmp.items():
    print("___Starting group {}:___".format(k))
    for vv in v:
        print(vv)

Prints:

___Starting group A:___
A1-A2
A3-A4
Andrej Kesely
  • 168,389
  • 15
  • 48
  • 91
  • Hey, thanks for your time. Unfortunately I don't mean to print that way. It has to be automated and I can't use group by by the fact that I have an index on the team and it has to be there. I mean that in combinations instead of 'Group == "{}"'.format(group) it should be 'Match == "{}"'.format(game(i.e)). The only problem I have with this is that I don't know how I can match the value of game – Kon Wes Nov 18 '22 at 11:04