-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
160 lines (137 loc) · 5.86 KB
/
Copy pathapp.py
File metadata and controls
160 lines (137 loc) · 5.86 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import os
import streamlit as st
import pandas as pd
import preprocessor, helper
import matplotlib.pyplot as plt
import seaborn as sns
import plotly.figure_factory as ff
import plotly.express as px
df = pd.read_csv(os.path.join("data", "athlete_events.csv"))
region_df = pd.read_csv(os.path.join("data", "noc_regions.csv"))
df = preprocessor.preprocess(df, region_df)
st.sidebar.title("Summer Olympic Analysis")
user_menu = st.sidebar.radio(
'Select an option',
('Medal Tally', 'Overall Analysis', 'Country-wise Analysis', 'Athelete-wise Analysis')
)
if user_menu == 'Medal Tally':
st.sidebar.header("Medal Tally")
years, country = helper.country_year_list(df)
selected_year = st.sidebar.selectbox("Select Year", years)
selected_country = st.sidebar.selectbox("Select Country", country)
medal_tally = helper.fetch_medal_tally(df, selected_year, selected_country)
if selected_country == 'Overall' and selected_year == 'Overall':
st.title('Overall Medal Tally')
if selected_country == 'Overall' and selected_year != 'Overall':
st.title(f"Medal Tally in {selected_year}")
if selected_country != 'Overall' and selected_year == 'Overall':
st.title(f"{selected_country} Overall Performance")
if selected_country != 'Overall' and selected_year != 'Overall':
st.title(f"{selected_country} Performance in {selected_year}")
st.table(medal_tally)
if user_menu == 'Overall Analysis':
editions = df['Year'].unique().shape[0]-1
cities = df['City'].unique().shape[0]
sports = df['Sport'].unique().shape[0]
events = df['Event'].unique().shape[0]
athelets = df['Name'].unique().shape[0]
nations = df['region'].unique().shape[0]
st.title("Stats")
col1,col2, col3 = st.columns(3)
with col1:
st.header("Editions")
st.title(editions)
with col2:
st.header("Hosts")
st.title(cities)
with col3:
st.header("Sports")
st.title(sports)
col1,col2, col3 = st.columns(3)
with col1:
st.header("Events")
st.title(events)
with col2:
st.header("Nations")
st.title(nations)
with col3:
st.header("Athelets")
st.title(athelets)
# Participating Nations' graph
nations_over_time = helper.data_over_time(df, 'region')
st.title("Participating Nations over time")
fig, ax = plt.subplots()
ax.plot(nations_over_time['Year'], nations_over_time['count'], marker='o')
ax.set_title("Participating nations over time")
ax.set_xlabel("Year")
ax.set_ylabel("Participating Nations")
st.pyplot(fig)
# Total Events over time
events_over_time = helper.data_over_time(df, 'Event')
st.title("No. of Events over time")
fig, ax = plt.subplots()
ax.plot(events_over_time['Year'], events_over_time['count'], marker='o')
ax.set_title("No. of Events over time")
ax.set_xlabel("Year")
ax.set_ylabel("Total Events")
st.pyplot(fig)
# Participating Atheletes over time
athelets_over_time = helper.data_over_time(df, 'Name')
st.title("No. of Participating Atheletes over time")
fig, ax = plt.subplots()
ax.plot(athelets_over_time['Year'], athelets_over_time['count'], marker='o')
ax.set_title("No. of Participating Atheletes over time")
ax.set_xlabel("Year")
ax.set_ylabel("Total Participating Atheletes")
st.pyplot(fig)
# Ploting heatmap
st.title("No. of Events per Sport over time")
fig, ax = plt.subplots(figsize=(25,25))
x = df.drop_duplicates(['Year', 'Sport', 'Event'])
ax = sns.heatmap(x.pivot_table(index='Sport', columns='Year', values='Event', aggfunc='count').fillna(0).astype('int'),annot=True)
st.pyplot(fig)
st.title("Most Successful Athletes")
sport_list = df['Sport'].unique().tolist()
sport_list.sort()
sport_list.insert(0, 'Overall')
selected_sport = st.selectbox('Select the Sport', sport_list)
top_athletes = helper.most_successful(df,selected_sport)
st.table(top_athletes)
if user_menu == 'Country-wise Analysis':
st.sidebar.title('Country-wise Analysis')
country_list = df['region'].dropna().unique().tolist()
country_list.sort()
selected_country = st.sidebar.selectbox('Select the country', country_list)
country_df = helper.yearwise_medal_tally(df, selected_country)
fig, ax = plt.subplots()
ax.plot(country_df['Year'], country_df['Medal'], marker='o')
ax.set_title(f"Medals Over Time - {selected_country}")
ax.set_xlabel('Year')
ax.set_ylabel('Medals Won')
st.pyplot(fig)
pt = helper.country_event_heatmap(df, selected_country)
st.title(f"Performance in Each Sport over time - {selected_country}")
if pt.empty:
st.warning("No data Available for this Country.")
else:
fig, ax = plt.subplots(figsize=(20,20))
ax = sns.heatmap(pt, annot=True)
st.pyplot(fig)
top_10 = helper.top_athletes_of_country(df, selected_country)
st.title(f'Top 10 athletes of {selected_country}')
st.table(top_10)
if user_menu == 'Athelete-wise Analysis':
# Remove duplicate names of Atheletess
st.title("Age Distribution of Atheletes")
athelete_df = df.drop_duplicates(subset=['Name', 'region'])
all_athelete_age = athelete_df['Age'].dropna()
gold_age = athelete_df[athelete_df['Medal'] == 'Gold']['Age'].dropna()
silver_age = athelete_df[athelete_df['Medal'] == 'Silver']['Age'].dropna()
bronze_age = athelete_df[athelete_df['Medal'] == 'Bronze']['Age'].dropna()
figure = ff.create_distplot([all_athelete_age, gold_age, silver_age, bronze_age], ['Overall Age', 'Gold Medalist', 'Silver Medalist', 'Bronze Medalist'], show_hist=False, show_rug=False)
figure.update_layout(autosize=False, width=1000, height=500)
st.plotly_chart(figure)
st.title("Men vs Women Participation")
final = helper.men_vs_women(df)
fig = px.line(final, x='Year', y=['Male', 'Female'])
st.plotly_chart(fig)