Home > Mobile >  Finding number of times a sport comes up for each country python data frame
Finding number of times a sport comes up for each country python data frame

Time:09-22

I am stuck on a problem about Olympics athletes. I need to find the number of different sports each country participates in from my data frame, I am unsure how to do this as the column 'country' obviously has duplicates as well as the column 'sport' and I cant figure out how to group them and return a value for each country

Any help would be awesome :)

CodePudding user response:

df.groupby(by=('country', 'sport')).count()

CodePudding user response:

import pandas as pd

df = pd.DataFrame([{'Country': 'India', 'Sport': 'Badmintan'}, {'Country': 'China', 'Sport': 'Badmintan'},{'Country': 'India', 'Sport': 'Football'}])

print(df)

print(df.groupby(by=('Country')).count())

Answer: Country
China 1 India 2

  • Related