Reputation: 91
I am trying to find teams that have lowest and highest number of yellow cards. My two codes worked well but did not check the same numbers. Denmark and Germany have 4 yellow cards. These codes only grabs first maximum number and print it. I found that nlargest method. It worked too. But, you have to write two or three. But, if i have largest dataset, nlargest is not good method. I did research. I did not find what I want. How can I write the code print two min or max number if they are the same?
print('Max Yellow card number:',soccer['Yellow Cards'].min(),'team name is',soccer.loc[soccer['Yellow Cards'].idxmin()].Team)
df_subset=soccer.set_index('Team')[['Yellow Cards']]
df1 = df_subset.min(axis=1)
print (df1)
print (df1.nlargest(1).reset_index(name='Top_Yellow_Cards'))
print (df1.nsmallest(2).reset_index(name='Top_Yellow_Cards'))
Dataset is here: https://github.com/jokecamp/FootballData/blob/master/Euro%202012/Euro%202012%20stats%20TEAM.csv
Upvotes: 1
Views: 100
Reputation: 862671
You can simplify code and use boolean indexing
with compare with min
and max
of column:
url = 'https://raw.githubusercontent.com/jokecamp/FootballData/master/UEFA_European_Championship/Euro%202012/Euro%202012%20stats%20TEAM.csv'
soccer = pd.read_csv(url)
#print (soccer)
cols = ['Team','Yellow Cards']
min1 = soccer.loc[soccer['Yellow Cards'] == soccer['Yellow Cards'].min(), cols]
print (min1)
Team Yellow Cards
2 Denmark 4
5 Germany 4
max1 = soccer.loc[soccer['Yellow Cards'] == soccer['Yellow Cards'].max(), cols]
print (max1)
Team Yellow Cards
7 Italy 16
Upvotes: 2