Vault #5: Boston Marathon
In this DISCOVERY Data Science Project, you will do real data science in less than an hour and you will earn this project's card to your collection when you fully complete this project! π
Data Sources: SCORE Sports Data Repository and Boston-Marathon-Data-Project
The Boston Marathon is an annual marathon hosted by several cities in the greater Boston area, located in eastern Massachusetts, United States. It is held on Patriots' Day, the third Monday of April. Considered one of the world's oldest and most prestigious marathons, it attracts runners from all over the globe to participate in its challenging course.
The SCORE Sports Data Repository is a curated collection of datasets across various sports, designed for use in statistics and data science education. This repository encourages exploration and analysis, organized by sport and educational topic, and is funded by the National Science Foundationβ (ScoreNetwork)β.
We will be analyzing the 2023 Boston Marathon runners dataset in this project. In addition to the 2023 dataset, we will be analyzing datasets from 2000 and 2010. These datasets are pulled from Boston-Marathon-Data-Project, a public GitHub repository containing Boston Marathon data from 1897-2019.
Importing The Data
The "results2000.csv", "results2010.csv", and "results2023.csv" files contain data for the 2000, 2010, and 2023 Boston Marathons. Import the datasets using pandas and save them to variables called df_2000, df_2010, df_2023.
βοΈ Test Case: Data Import
Part 1: Longitudinal Analysis of Finish Times
In this puzzle, we're going to explore how the average, median, and variability of finish times have changed over the decades.
Puzzle 1.1: Creating an integer pace column in each dataframe
In order to use all of the datasets together, we need to clean them up and add new columns. The dataset from 2000 and 2010 have a string pace column that contains a string of each runner's average pace per mile in minutes (ex: "00:04:56"). The 2023 dataset does not have a pace column.
We can observe this in the datasets:
Create a pace_seconds Column
A marathon is exactly 26.21875 miles, so if we want to get the average seconds per mile we would divide the total seconds by 26.21875. Using the seconds column in the 2000 and 2010 dataset, create a column for the pace in seconds in each dataset. Call this column pace_seconds:
Using the "finish_net_sec" column in the 2023 dataset, create a column for the pace in seconds. Call this column pace_seconds.
βοΈ Test Case: Puzzle 1.1
Puzzle 1.2: Creating pace_minutes
At this point, you have normalized the datasets so that all three contain pace_seconds! π
Instead of trying to understand how long 506 seconds take, we want to add a new column called pace_minutes. This new column should just take the pace_seconds and divide it by 60 to get the runner's pace in minutes:
βοΈ Test Case: Puzzle 1.2: Creating pace_minutes
Let's take a look at the fastest runner in 2023:
Verify your DataFrame is Correct: You should find that Evans Chebet ran at a pace of 4.8 minutes -- he completed 26.2 miles at an average pace of just 4 minutes and 48 seconds (4.802 minutes)! That's insanely fast!!
Puzzle 1.3: Creating an Average Paces by Age Group DataFrame
In addition to pace_minutes you just added, each dataset also contains the age_group that contains a string value of the age group of each participant (ex: 18-39, 40-44, etc).
We want to explore if the average time in each age group has changed significantly throughout the years. To do this, we want to create a DataFrame with the following format:
| 2000 | 2010 | 2023 | |
|---|---|---|---|
| 18-39 | 8.31 | 8.52 | 8.07 |
| 40-44 | 8.36 | 8.63 | 8.23 |
| ... | ... | ... | ... |
There are many ways to organize your data like this. One of the most natural ways is to join the normalized datasets and create a pivot table.
Creating a Pivot Table: Preparing the Data
A pivot table is a summary of a DataFrame created by describing:
- The desired grouping of the rows in the result (the
indexparameter) -- in this case, each row is anage_group, - The desired grouping of the columns in the result (the
columnsparameter) -- in this case, each column is a year, - The desired data for each cell (the
valuesparameter) -- in this case, each cell containspace_minutes, AND - The method how we combine multiple
valuestogether if there's multiple values (ex:mean,max,sum, etc)
We will want to summarize all of our data, so we need to combine our three DataFrames into a single DataFrame. Before we do that, currently, each of our DataFrames do not contain data about the year that the data contained in the DataFrame is about.
Add a new column year to all three DataFrames that specifies the year in which the DataFrame is from:
Now, concatenate all three DataFrames together to create one large DataFrame called df:
Not sure how to combine the DataFrames? The DISCOVERY guide Combining DataFrames by Concatenation can help you out here!
βοΈ Test Case: Puzzle 1.3: Creating a Pivot Table: Preparing the Data
Creating a Pivot Table: Create the Pivot Table
Finally, let's create the pivot table!
You will need to identify the four features we need in the pivot table. The first three parameters (index, columns, and values) is the name of the column in the DataFrame. Refer back to the beginning of this section for a reminder of the purpose of these three parameters.
Since we're calculating the average pace for each age group, the aggfunc is provided for you as "mean". Complete the below code to create the summary:
βοΈ Test Case: Puzzle 1.3: Creating a Pivot Table: Create the Pivot Table
Puzzle 1.4: Visualizing the Result
Using your summary DataFrame, create a simple line chart using summary.plot.line() that is provided below:
Does it seem like marathon runners have improved over the years?
Part 2: Visualizing the Fastest Finish
Modify the code you used to create a summary to create a new summary_fastest that, instead of finding the average pace, find the fastest pace by each age group for each year. (Hint: aggfunc may help you here!)
Visualize the Result
Let's see if this data looks different using the same visualization:
βοΈ Test Case: Puzzle 2: Fastest Time
Validate and Complete This Project!
Congratulations on finishing this DISCOVERY Data Science Project! ππ
To validate your entire project, your entire code will run from top-to-bottom on this page and each test case will be validated one final time. If everything looks good, you'll earn the card for completing this project: