我在Pandas中有一个数据框,如下所示,其中存储了获胜球队的名称和获胜的体育场的名称,如下所示:
d={"Winner":["Team1","Team2","Team3","Team1","Team2","Team1"],
"Stadium":["S1","S2","S3","S1","S2","S1"]}
score=pd.DataFrame.from_dict(d)
现在我已经对上述数据框进行了分组计算,以找出每个球队的人数和在体育场赢得的比赛:
score.groupby(by=["Stadium","Winner"]).size().reset_index()
现在,我想将这些计数值添加回我的数据框中。
我面临多个按列分组的问题。
任何帮助。
答案 0 :(得分:2)
merge
的用途是
score.merge(
score.groupby(by=["Stadium","Winner"]).size().reset_index(),
on=["Stadium", "Winner"]
)