-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmedals.py
76 lines (57 loc) · 2.33 KB
/
medals.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import numpy as np
import pandas as pd
def show(df):
# dropping all the columns where the medals would be same for team event
mt = df.drop_duplicates(
subset=['Team', 'NOC', 'Games', 'Year', 'Season', 'City', 'Sport', 'Event', 'Medal'])
# grouping the medals based on country
mt = mt.groupby('Country').sum()[['Gold', 'Silver', 'Bronze']].sort_values(
'Gold', ascending=False).reset_index()
# creating a total column
mt['Total'] = mt['Gold'] + mt['Silver'] + mt['Bronze']
mt['Gold'] = mt['Gold'].astype(int)
mt['Silver'] = mt['Silver'].astype(int)
mt['Bronze'] = mt['Bronze'].astype(int)
mt['Total'] = mt['Total'].astype(int)
return mt
def country_year_list(df):
# for years
year = df['Year'].unique().tolist()
year.sort()
year.insert(0, 'Overall')
# for countries
country = df['Country']
country = country.dropna().values
country = np.unique(country).tolist()
country.sort()
country.insert(0, 'Overall')
return year, country
def get_medal_tally(df, year, country):
medal_df = df.drop_duplicates(
subset=['Team', 'NOC', 'Games', 'Year', 'Season', 'City', 'Sport', 'Event', 'Medal'])
flag = 0
if year == 'Overall' and country == 'Overall':
temp_df = medal_df
if year == 'Overall' and country != 'Overall':
flag = 1
temp_df = medal_df[medal_df['Country'] == country]
if year != 'Overall' and country == 'Overall':
temp_df = medal_df[medal_df['Year'] == int(year)]
if year != 'Overall' and country != 'Overall':
temp_df = medal_df[(medal_df['Year'] == int(year))
& (medal_df['Country'] == country)]
if flag == 1:
# grouping this medal tally according to year
t = temp_df.groupby('Year').sum()[['Gold', 'Silver', 'Bronze']].sort_values(
'Year', ascending=True).reset_index()
else:
# grouping this medal tally according to country
t = temp_df.groupby('Country').sum()[['Gold', 'Silver', 'Bronze']].sort_values(
'Gold', ascending=False).reset_index()
# getting total medals
t['Total'] = t['Gold'] + t['Silver'] + t['Bronze']
t['Gold'] = t['Gold'].astype(int)
t['Silver'] = t['Silver'].astype(int)
t['Bronze'] = t['Bronze'].astype(int)
t['Total'] = t['Total'].astype(int)
return t