-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
203 lines (155 loc) · 5.77 KB
/
app.py
File metadata and controls
203 lines (155 loc) · 5.77 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import streamlit as st
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
st.set_page_config(
page_title="Olympic Data Analysis", # setting page title and favicon
page_icon="🏅",
layout="wide", # setting it to display in the wide layout
)
@st.cache_data # making the data cached so we don't load the dataset all the time.
def load_data(filepath):
return pd.read_csv(filepath)
@st.cache_data
def preprocess_data(data):
# Rename columns and drop missing lat/lon rows
data = data.rename(
columns={"Latitude": "latitude", "Longitude": "longitude"}
) # Streamlit requires the latitude column to have specific names like LAT, LATITUDE, lat, or latitude
data = data.dropna(
subset=["latitude", "longitude"]
) # removes rows from the DataFrame df where the values in the columns "latitude" or "longitude" are NaN (missing).
return data
@st.cache_data
def get_medal_count(data):
medal_count = (
data.groupby("region")["Medal"].count().sort_values(ascending=False).head(10)
)
# Reset the index to convert the Series to a DataFrame
medal_count = medal_count.reset_index()
return medal_count
@st.cache_data
def get_country_data(data):
medal_counts = df.groupby(["Year", "Team"])["Medal"].count().reset_index()
top_countries = (
medal_counts.groupby("Team")["Medal"]
.sum()
.sort_values(ascending=False)
.head(10)
.index
)
top_countries_data = medal_counts[medal_counts["Team"].isin(top_countries)]
return top_countries_data
###################################################################################################################################################
st.title("Olympic Data Analysis (1896 - 2016) 🏅")
st.markdown(
"A Historical analysis of the world's largest sporting event over a span of 120 years. Full analysis notebook ➡️ [notebook](https://github.com/subhanu-dev/Olympic-Data-Analysis/blob/main/Olympic%20Data%20Analysis.ipynb)"
)
# loading the data
df = load_data("data/merged_df.csv")
# setting columns
col1, col2 = st.columns([2, 1])
############## filtering data and select box
filtered_data = preprocess_data(df)
with col1:
col1_1, col1_2, col1_3 = st.columns(3) # defining sub columns
with col1_1:
year = st.selectbox(
"Select Year",
sorted(filtered_data["Year"].unique(), reverse=True),
index=None,
)
# filtering data only if we select a specific year or else we have the entire dataset
if year is not None:
filtered_data = filtered_data[filtered_data["Year"] == year]
with col1_2:
regions = len(filtered_data["region"].unique())
st.metric(label="Number of Countries", value=regions)
with col1_3:
sum = filtered_data["Name"].nunique()
st.metric(label="Number of Contestants", value=sum)
################### plotting map
st.markdown("### Participating Regions")
# Plotting the map with the filtered data
st.map(filtered_data[["latitude", "longitude"]])
############## plotting the bar graph of top countries
medal_count = get_medal_count(filtered_data)
sns.set_theme(style="whitegrid")
# Creating the map
fig, ax = plt.subplots(figsize=(10, 4))
sns.barplot(x="region", y="Medal", data=medal_count, palette="viridis", ax=ax)
plt.xlabel("Countries")
plt.ylabel("Number of Medals")
plt.xticks(rotation=45)
st.subheader("Top 10 Countries Winning Medals")
st.pyplot(fig)
st.markdown("<br>", unsafe_allow_html=True)
medal_count.index = range(1, len(medal_count) + 1)
st.dataframe(medal_count.T)
with col2:
##############age distribution
st.subheader("Age Distribution")
fig, ax = plt.subplots(figsize=(10, 6))
plt.title("Athletes Age Distribution")
plt.xlabel("Age")
plt.ylabel("Number")
# Plot using seaborn
sns.histplot(
data=filtered_data,
x="Age",
bins=np.arange(10, 80, 2),
color="#099e50",
edgecolor="red",
)
# Show the plot in Streamlit
st.pyplot(fig)
######gender distribution
st.subheader("Gender Distribution")
fig, ax = plt.subplots(figsize=(6, 6))
filtered_data["Sex"].value_counts().plot(
kind="pie",
autopct="%1.2f%%",
ax=ax,
colors=["#ffa04c", "#099e50"],
shadow=True,
explode=(0.05, 0.1),
)
# Displaying top athletes in the history of olympics
top_athletes = (
df.groupby(["Name", "Sex"])["Medal"]
.count()
.sort_values(ascending=False)
.reset_index()
.head(10) # Select the top 10
)
ax.set_ylabel("") # Hiding the y-axis label
st.pyplot(fig)
# Rename columns for better display
top_athletes.columns = ["Athlete Name", "Sex", "Medal Count"]
st.subheader("Top 10 Athletes by Medal Count")
top_athletes.index = range(1, len(top_athletes) + 1)
st.dataframe(top_athletes)
st.markdown("<br>", unsafe_allow_html=True)
st.subheader("Top 10 Countries' Olympic Performance - Against Time ")
# Add a slider to the sidebar:
selected_years = st.slider("Select a range of years", 1896, 2016, (2000, 2016))
top_countries_data = get_country_data(df)
specified_data = top_countries_data[
top_countries_data["Year"].between(selected_years[0], selected_years[1])
]
fig, ax = plt.subplots(figsize=(18, 4))
sns.lineplot(
data=specified_data,
x="Year",
y="Medal",
hue="Team",
ax=ax,
)
plt.xlabel("Year")
plt.ylabel("Number of Medals")
plt.legend(fontsize=10, loc="upper right")
st.pyplot(plt)
########################################################################################################
st.markdown("---")
st.markdown("Made with ❤️ by [Subhanu](https://github.com/subhanu-dev)")