Open In App

Olympics Data Analysis Using Python

Last Updated : 13 May, 2022
Like Article

In this article, we are going to see the Olympics analysis using Python. The modern Olympic Games or Olympics are leading international sports events featuring summer and winter sports competitions in which thousands of athletes from around the world participate in a variety of competitions. The Olympic Games are considered the world’s foremost sports competition with more than 200 nations participating.

The total number of events in the Olympics is 339 in 33 sports. And for every event there are winners. Therefore various data is generated. So, by using Python we will analyze this data.

Modules Used

  • Pandas: It is used for analyzing the data,
  • NumPy: NumPy is a general-purpose array-processing package.
  • Matplotlib: It is a numerical mathematics extension NumPy
  • seaborn: It is used for visualization statistical graphics plotting in Python

Model architecture :  

Stepwise Implementation  

Step 1: Importing libraries


# importing all necessary libraries
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

Step 2: Understanding database  

When dealing with Olympic data, we have two CSV files. One containing outturn sports-related costs of the Olympic Games of all years. And other is containing the information about athletes of all years when they participated with information.

CSV data file can be download from here: Datasets

Step 3: Data cleaning and formatting

We imported both the datasets using the .read_csv() method into a dataframe using pandas and displayed the first 5 rows of each dataset.


data = pd.read_csv('athlete_events.csv'# read file
# data.head() display first 5 entry
# data.describe  about model
# give info about data
print(data.head(), data.describe(),


Step 4: Merging two DataFrame

Here we are going to merge two dataframe using pandas.merge() in python.


# regions and country noc data csv file
regions = pd.read_csv('datasets_31029_40943_noc_regions.csv')
# merging to data and regions frame
merged = pd.merge(data, regions, on='NOC', how='left')


Data analysis of Olympics

Data is now available now using pandas and matplotlib lets see some examples

Data analysis of Gold medalists

Creating a new data frame including only gold medalists.


# creating goldmedal dataframes
goldMedals = merged[(merged.Medal == 'Gold')]

Output : 

Gold medalist in respect of age:

Here we are going to create a graph of the number of gold medals with respect to age. For this, we will create countplot for graph representation which shows the X-axis as the age of the players and the Y-axis represent the number of medals.


plt.figure(figsize=(20, 10))
plt.title('Distribution of Gold Medals')

Output : 

Print the number of athletes who are gold medalists and whose age is greater than 50 with their info.


goldMedals = merged[(merged.Medal == 'Gold')]
print('The no of athletes is',
      goldMedals['ID'][goldMedals['Age'] > 50].count(), '\n')
print(goldMedals[goldMedals['Age'] > 50])

Output : 

Create a new dataframe called masterDisciplines in which we will insert this new set of people and then create a visualization with it


masterDisciplines = goldMedals['Sport'][goldMedals['Age'] > 50]
plt.figure(figsize=(20, 10))
plt.title('Gold Medals for Athletes Over 50')

Output :

Women who can play in summer

Display all women athletes who have played in the summer season and it show the increase in women athletes after a long period via graphical representation.


womenInOlympics = merged[(merged.Sex == 'F') &
                         (merged.Season == 'Summer')]
plt.figure(figsize=(20, 10))
sns.countplot(x='Year', data=womenInOlympics)
plt.title('Women medals per edition of the Games')

Output : 

Top  5 countries who won the most medals

Here we are going to print the top 5 countries and show them in the graph with catplot.


totalGoldMedals = goldMedals.region.value_counts()
g = sns.catplot(x="index", y="Medal", data=totalGoldMedals,
                height=6, kind="bar", palette="muted")
g.set_xlabels("Top 5 countries")
g.set_ylabels("Number of Medals")
plt.title('Medals per Country')


Players weight Analysis

Here we are going to see how weight over year for Male Lifters via graphical representation using pointplot.


MenOverTime = merged[(merged.Sex == 'M') &
                     (merged.Season == 'Summer')]
wlMenOverTime = MenOverTime.loc[MenOverTime['Sport'] == 'Weightlifting']
plt.figure(figsize=(20, 10))
sns.pointplot('Year', 'Weight', data=wlMenOverTime, palette='Set2')
plt.title('Weight over year for Male Lifters')

Output : 

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads