0

I have a dataframe in Pandas as below which is storing name of Team won and Name of stadium where it won as below

d={"Winner":["Team1","Team2","Team3","Team1","Team2","Team1"],
         "Stadium":["S1","S2","S3","S1","S2","S1"]}

score=pd.DataFrame.from_dict(d)

Now I have performed some calculation with group by on above data frame to find count of each team and match won at a stadium as :

 score.groupby(by=["Stadium","Winner"]).size().reset_index()

Now, I want to add these count values back to my data frame.

I am facing issue with multiple group by columns.

Any help.

dev
  • 732
  • 2
  • 8
  • 29

1 Answers1

2

This is what merge is for:

score.merge(
    score.groupby(by=["Stadium","Winner"]).size().reset_index(),
    on=["Stadium", "Winner"]
)
Denziloe
  • 7,473
  • 3
  • 24
  • 34