用groupby获得并用transform重塑的每个国家和球队的比赛数量之和除以数据帧。在df = pd.DataFrame({'country': ["NL"] * 9,
'team': ["A"] * 3 + ["B"] * 3 + ["C"] * 3,
'outcome': ["WIN", "LOSE", "DRAW"] * 3,
'week1': [2, 3, 4, 4, 5, 2, 4, 4, 2],
'week2': [3, 2, 5, 2, 3, 4, 2, 3, 4],
'week3': [4, 5, 2, 3, 2, 5, 3, 2, 5]})
df.set_index(['country', 'team', 'outcome'], inplace=True)
>>> df.divide(df.reset_index().groupby(['country', 'team']).transform(sum).values)
week1 week2 week3
country team outcome
NL A WIN 0.222222 0.300000 0.363636
LOSE 0.333333 0.200000 0.454545
DRAW 0.444444 0.500000 0.181818
B WIN 0.363636 0.222222 0.300000
LOSE 0.454545 0.333333 0.200000
DRAW 0.181818 0.444444 0.50000