After using .groupby(['match_id', 'team']).sum()
I'm left with this multi-index dataframe:
visionScore
match_id team
EUW1_5671848066 blue 212
red 127
EUW1_5671858853 blue 146
red 170
EUW1_5672206092 blue 82
... ...
How do I add a new boolean column that will tell whether blue or red team has larger visionScore
? If there's a draw, consider both teams to be winning.
This would work:
import pandas as pd
df = pd.DataFrame(
{"visionScore": [212, 127, 146, 170, 82, 82]},
index=pd.MultiIndex.from_product([["EUW1_5671848066", "EUW1_5671858853", "EUW1_5672206092"], ["blue", "red"]], names=["match_id", "team"])
)
df["winner"] = df.groupby("match_id").transform(lambda x: [x[0] >= x[1], x[1] >= x[0]])
# df:
# visionScore winner
# match_id team
# EUW1_5671848066 blue 212 True
# red 127 False
# EUW1_5671858853 blue 146 False
# red 170 True
# EUW1_5672206092 blue 82 True
# red 82 True
though I can't help but think that there's a better way ,:)