generated from mine-cetinkaya-rundel/olympicdash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patholympicdash-py-1.quarto_ipynb
188 lines (188 loc) · 6.26 KB
/
olympicdash-py-1.quarto_ipynb
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
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"---\n",
"title: \"Olympic Games\"\n",
"format: html\n",
"---"
],
"id": "e89a4c15"
},
{
"cell_type": "code",
"metadata": {},
"source": [
"#| label: load-packages\n",
"from plotnine import *\n",
"import great_tables as gt\n",
"import pandas as pd"
],
"id": "load-packages",
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {},
"source": [
"#| label: load-data\n",
"olympics_full = pd.read_csv(\"data/olympics.csv\", low_memory = False)"
],
"id": "load-data",
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {},
"source": [
"#| label: prep-data\n",
"# Filter for Summer season and non-NA medals\n",
"olympics = olympics_full[(olympics_full[\"season\"] == \"Summer\") & (olympics_full[\"medal\"].notna())]\n",
"\n",
"# Split the team column at \"-\" into two columns\n",
"split_data = olympics[\"team\"].str.split(\"-\", n = 1, expand = True)\n",
"olympics.loc[:, \"team\"] = split_data[0]\n",
"\n",
"# Reorder the medal column categories\n",
"olympics.loc[:, \"medal\"] = pd.Categorical(olympics[\"medal\"], categories = [\"Bronze\", \"Silver\", \"Gold\"])"
],
"id": "prep-data",
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Medals by sport\n"
],
"id": "f954d5e9"
},
{
"cell_type": "code",
"metadata": {},
"source": [
"#| warning: false\n",
"# Lump the sport column to top 15 categories, grouping others as Other\n",
"top_15_sports = olympics[\"sport\"].value_counts().nlargest(15).index\n",
"olympics[\"sport\"] = olympics[\"sport\"].apply(lambda x: x if x in top_15_sports else \"Other\")\n",
"\n",
"# Convert the sport column to a categorical type with order based on frequency, and reverse the order\n",
"olympics[\"sport\"] = pd.Categorical(olympics[\"sport\"], categories = olympics[\"sport\"].value_counts().index[::-1])\n",
"\n",
"# Move the Other category of the sport column to the beginning\n",
"new_order = [\"Other\"] + [cat for cat in olympics[\"sport\"].cat.categories if cat != \"Other\"]\n",
"olympics[\"sport\"] = olympics[\"sport\"].cat.reorder_categories(new_order)\n",
"\n",
"# Plot\n",
"(\n",
" ggplot(olympics, aes(x = \"sport\", fill = \"medal\"))\n",
" + geom_bar()\n",
" + coord_flip()\n",
" + guides(fill = guide_legend(reverse = True)) \n",
" + labs(x = \"\", y = \"\", fill = \"Medal\") \n",
" + theme_minimal() \n",
" + theme(\n",
" legend_position = \"inside\",\n",
" legend_position_inside = (0.9, 0.2),\n",
" legend_direction = \"horizontal\",\n",
" legend_background = element_rect(fill = \"white\", color = \"gray\"),\n",
" figure_size = (10, 5)\n",
" )\n",
")"
],
"id": "45ebd02e",
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Medals by year\n"
],
"id": "d235201c"
},
{
"cell_type": "code",
"metadata": {},
"source": [
"# Count the occurrences of each medal per year\n",
"olympics_count = olympics.groupby([\"year\", \"medal\"], observed=True).size().reset_index(name = \"n\")\n",
"\n",
"# Plot\n",
"(\n",
" ggplot(olympics_count, aes(x = \"year\", y = \"n\", color = \"medal\")) \n",
" + geom_point(size = 0.5) \n",
" + geom_line() \n",
" + guides(color = guide_legend(reverse = True)) \n",
" + scale_x_continuous(breaks = range(1896, 2020, 8)) \n",
" + labs(x = \"Year\", y = \"\", color = \"Medal\") \n",
" + theme_minimal() \n",
" + theme(\n",
" legend_position = \"inside\",\n",
" legend_position_inside = (0.9, 0.2),\n",
" legend_direction = \"horizontal\",\n",
" legend_background = element_rect(fill = \"white\", color = \"gray\"),\n",
" figure_size = (10, 2.5)\n",
" )\n",
")"
],
"id": "262b4401",
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Medals by country\n"
],
"id": "a1cda28e"
},
{
"cell_type": "code",
"metadata": {},
"source": [
"# Count the occurrences of each medal per team\n",
"olympics_count = olympics.groupby([\"team\", \"medal\"]).size().reset_index(name=\"n\")\n",
"\n",
"# Pivot olympics_count to get medals as columns\n",
"olympics_pivot = olympics_count.pivot_table(index = \"team\", columns = \"medal\", values = \"n\", fill_value = 0)\n",
"\n",
"# Calculate the total number of medals\n",
"olympics_pivot[\"Total\"] = olympics_pivot[[\"Bronze\", \"Gold\", \"Silver\"]].sum(axis=1)\n",
"\n",
"# Reset the index and rearrange columns\n",
"olympics_pivot = olympics_pivot.reset_index()\n",
"olympics_pivot = olympics_pivot[[\"team\", \"Gold\", \"Silver\", \"Bronze\", \"Total\"]]\n",
"\n",
"# Sort by Total medals, then team\n",
"olympics_pivot = olympics_pivot.sort_values(by=[\"Total\", \"team\"], ascending=[False, True])\n",
"\n",
"# Rename the team column to Team\n",
"olympics_pivot.rename(columns={\"team\": \"Team\"}, inplace=True)\n",
"\n",
"(\n",
" gt.GT(olympics_pivot)\n",
")"
],
"id": "4f2a788e",
"execution_count": null,
"outputs": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
}
},
"nbformat": 4,
"nbformat_minor": 5
}