0

The problem I'm having right now is seperating a groupby.

I have a dataframe of Olympic data which would look something like this for example:

ID       Name        Sport        Medal
1        name1     Athletics       Gold
2        name2      Handball       NaN
3        name3      Fencing       Silver
4        name4     Athletics       NaN
5        name5      Fencing       Bronze
6        name6      Fencing        Gold

And I want to show the amount of medals per sport. I tried to achieve this by using:

medals_per_sport = df.groupby(['Sport', 'Medal']).size()
medals_per_sport

This gives me the following result:

    Sport           Medal
   Athletics        Gold     1
   Fencing          Silver   1
                    Bronze   1
                    Gold     1

This looks good, but what I would kinda want is a dataframe result like this:

    Sport       Gold        Silver       Bronze
  Athletics      1          NaN/0         NaN/0
  Fencing        1            1             1

So I can easily get the amount of medals per sport. Or (maybe I'm missing the use of a "Series") someone could show me a way to easily extract this information from the above Series.

Déjà vu
  • 774
  • 2
  • 9
  • 31

0 Answers0