# -*- coding: utf-8 -*-
"""olympic-eda.ipynb

Automatically generated by Colab.

Original file is located at
    https://colab.research.google.com/drive/1aWl38RgM3Tt1B7xudtC3qe5Z_DWtuigJ

## 📥 Importing Libraries
"""

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import plotly.express as px
from matplotlib.colors import ListedColormap
from wordcloud import WordCloud

"""## 🎨Setting The Style And Color Palette"""

#Lets set the style of all our seaborn based plots
plt.style.use(['ggplot'])
# Setting the theme of our plots
theme = ["#0a2e36", "#27FB6B","#14cc60","#036d19","#09a129"]
print('THEME')
sns.palplot(sns.color_palette(theme))
palette= sns.set_palette(sns.color_palette(theme))

"""## 🧹 Data Preparation and Cleaning
1. Load the file using pandas
2. Look for some of the information about the data and the columns
3. Fix any of the missing or incorrect values
"""

path_1 = './data/athlete_events.csv'
df_1 = pd.read_csv(path_1)
df_1.head()

path_2 = './data/noc_regions.csv'
df_2 = pd.read_csv(path_2)
df_2.head()

"""The focus of this EDA project will solely be on the "Summer Olympics", Let us filter of all the "Winter Olympics Games" from our dataset and perform some basic analysis on dataset `df_1`"""

df_1= df_1[df_1["Season"]=="Summer"]
df_1.head().style.background_gradient(cmap='Greens',axis=None)

# Lets check the columns of our dataset
df_1.columns

# Lets check the number of columns in our dataset
len(df_1.columns)

# Lets see the number of rows the dataset has
len(df_1)

# Summary of the dataset
df_1.info()

df_1.describe().style.background_gradient(cmap='Greens',axis=None)

"""Here's something that caught my eye from the above statistical values:
- In the `Age` column, the minimum value is `10`, meaning that a kid as young as 10 years has participated in the one of the biggest sporting events on the planet.
- The youngest documented Olympian is 10-year-old Greek gymnast who goes by the name Dimitrios Loundras. He had participated in the 1896 Greek Olympics and managed to bag a Bronze Medal too!

We will use same methods to analyze the `df_2` dataset
"""

df_2.columns

len(df_2.columns)

len(df_2)

df_2.info()

df_2.describe().style.set_properties(**{'background-color': '#DEF5E5',
                                    'color': 'black',
                                    'border': '0.5px  solid black'})

"""## 🔀 Merging The Two Datasets Into One"""

data_df= pd.merge(df_1, df_2, how='left',on='NOC')
data_df.head().style.background_gradient(cmap='Greens',axis=0)

"""## ⚠️ Finding and Replacing The Null Values In Our Dataset"""

data_df.isnull().sum()

"""Now, let us make some insightful visualisations and checkout the percentage of the data that is actually missing in the dataset."""

missing_percentage= 100*(data_df.isna().sum().sort_values(ascending=False)/len(data_df))
missing_percentage[missing_percentage!=0]

plt.title("Percentage Of Missing Values: Horizontal Bar Graph")
missing_percentage.plot(figsize=(10,6),kind="bar",grid=True,cmap='ocean');

plt.title("Percentage Of Missing Values: Pie Chart")
missing_percentage.plot(figsize=(12,6),kind="pie",cmap="ocean");

"""The notes column in this dataframe is not of much use to our EDA project so let us just remove it."""

data_df.drop(["notes"],axis=1,inplace=True)

"""Now let us fill all the null values in columns `Age`, `Height` and `Weight` with the mean column parameters."""

data_df["Age"].fillna(data_df["Age"].mean(),inplace=True)
data_df["Height"].fillna(data_df["Height"].mean(),inplace=True)
data_df["Weight"].fillna(data_df["Weight"].mean(),inplace=True)

data_df["region"].unique()

"""Let us replace all the Null values in the region dataframe with a string that reads "Region Unknown"."""

data_df["region"].fillna("Region Unknown",inplace=True)

"""Since only the winners of their respective events will have either a `Gold`, a `Silver` or a `Bronze` medal against their names, Let us fill all the `Null Values` in the dataframe with a string that reads `"Medal Not Won"`."""

data_df['Medal'].fillna(value="Medal Not Won",inplace=True)
data_df.head().style.background_gradient(cmap='Greens',axis=0)

data_df.isnull().sum()

"""Now let us check if our dataset has any duplicate values."""

data_df.duplicated()

data_df.duplicated().sum()

"""Let's drop these duplicate values using the `.drop_duplicates()` method. The argument `keep = 'First'`will ensure that pandas deletes all the duplicate rows, but for the first one!"""

data_df.drop_duplicates(keep='first',inplace=True)
data_df.duplicated().sum()

"""Before we head to the `Data Visualisation` section, there is a anomaly that requires our attention. To showcase this anomaly, let me get all the all-time gold medals won by India in the Olympics"""

data_df[(data_df["Team"]=="India") & (data_df["Medal"]=="Gold")].head()

len(data_df[(data_df["Team"]=="India") & (data_df["Medal"]=="Gold")])

"""- 131 gold medals! Now a quick Google search will return the fact that India has not won over 131 Gold medals in the Olympics.
- So is our dataset wrong? Well not exactly, here, instead of tallying the medal of a team event as one medal to the nation, `Pandas` is counting and giving us a summation of all the individials who were a part of the team.
- For now let us leave this as it as. This will not have a big impact on our visulisations as very `country` in this dataset will have this advantage!

## 📈 Exploratory Analysis and Visualisations

#### 1. A Word-Cloud That Graphically Shows The  Nations Have Sent Maximum Number Of Athletes Over The Years
"""

# We will first join all the instances of all the teams into a single string and store it in the variable "countries".
countries = " ".join(n for n in data_df['Team'])
plt.figure(figsize=(12, 11))
wc = WordCloud(background_color='black',colormap='gist_rainbow',collocations=False).generate(countries)
plt.imshow(wc, interpolation='bilinear')
plt.axis('off')
plt.show()

"""This indicates that we have more players from United States, Great Britain, France, Germany, Spain, Italy, Japan, Canada, Netherlands etc among other places."""

#Lets verify the above word cloud using this simple code:
count= data_df['Team'].value_counts()
count.head(20)

"""#### 2. The Relation Between Various Features And Labels In The Olympics Dataset

For checking the various trends between various parameters in our dataset, we will use the pairplot graph. Note that in case of large datasets, it may become difficult to make any solid inferences from the pairplot. Nevertheless, lets give it a try and see what output we get!
"""

sns.pairplot(data_df,palette="theme")

"""**Inference**: Well, we can clearly see a sort of a trend in the relationship between various parameters in our dataset.
- For example: We can see that trend in height and weight has increased over the years. However the age of the participants has been almost uniform (consistant) over the years.
- Now, lets go ahead and explore each one of them!

#### 3. The Overall Spread Of The Age Of Athletes In The Summer Olympics
"""

line_colors=[ "#14cc60","#036d19","#09a129"]
fig = px.box(data_df, x="Year", y="Age",title="<b>The Overall Trend Of Athelete's Age</b>",color_discrete_sequence=line_colors,template = "none")
fig.update_layout(plot_bgcolor = " whitesmoke")
fig.show()

"""**Inference:**
- We see that the median age of athletes has been around 25 years for most of the Olympic games over period of 100 years.
- We also see some outliers in the data, which are the athletes who have participated in the Olympics at the age of 10 years and there are 2 people who have participated at the age of 96 and 97 years.

#### 4. The Overall Trend Of The Summer Olympics Over The Years
"""

trend_df= data_df.groupby('Year').count()['ID'].reset_index()
trend_df.rename(columns={"ID":"Count"},inplace=True)
trend_df.head()

line_colors=[ "#14cc60","#036d19","#09a129"]
fig= px.line(trend_df,x='Year',y='Count',title="<b>The Variation In Participants Over The Years</b>",markers=True,color_discrete_sequence=line_colors,template = "none")
fig.update_layout(plot_bgcolor = " whitesmoke")
fig.show()

line_colors=["#09a129"]
fig=px.histogram(trend_df,x='Year',y='Count',title="<b>The Variation In Participants Over The Years</b>",nbins=70,color_discrete_sequence=line_colors,template = "none")
fig.update_layout(plot_bgcolor = " whitesmoke")
fig.show()

"""**Inference:** Even though we see that the overall trends has been gradually tending upwards over the years, we can clearly see that there is a sudden drop in the number of participants in the years 1932, 1956 and 1980.

#### 5. The Variation of Female Participants Over The Years In The Olympics
"""

female_df= data_df[data_df["Sex"]=="F"]
female_df.head()

plt.style.use(['classic'])
plt.figure(figsize=(20, 10))
sns.countplot(x='Year', data=female_df, palette=theme)
plt.title('Women medals per edition of the Games', fontsize=20)

"""**Inference:** We see that the participation of women has been consistently on the rise over the years.

#### 6. The Variation of Female Participants In Comparsion To Male Participants Over The Years
"""

gender_trend_df=data_df.groupby(['Sex','Year']).count().reset_index()
gender_trend_df.head(20).style.background_gradient(cmap='Greens',axis=0)

line_colors = ["#0a2e36",'#27fb6b']
a=px.line(gender_trend_df,x="Year",y="ID",color='Sex',markers=True,color_discrete_sequence=line_colors,template = "none")
a.update_layout(plot_bgcolor = "whitesmoke",
    title="The Male Vs Female Participants Trend Over The Years",
    yaxis_title="Number Of Participants"
)
a.show()

line_colors = ["#0a2e36",'#27fb6b']
fig = px.histogram(gender_trend_df, x="Year",y="ID",color='Sex',nbins=30, opacity=1,color_discrete_sequence=line_colors,template = "none")

fig.update_layout(plot_bgcolor = " whitesmoke",
    title="<b>The Male Vs Female Participants Trend Over The Years</b>",
    yaxis_title="Number Of Participants"
)
fig.show()

"""**Inference:** Even though the overall trend of both the graphs is on the rise. However, after the 1996 we see that there was a slight dip in the number of male participants.

#### 7. The Relationship Between Height Vs Weight Vs Age of Participants Across Sports
"""

plot = px.scatter(data_df, x="Height", y="Weight",size = "Age", color='Sport',title="<b>Variation Of Height Vs Weight Vs Age Across Sports</b>")
plot.update_layout(plot_bgcolor = "Black",autosize=True)
plot.show()

"""**Inference:** On careful observation we can see that `Height` and the `Weight` of the participants follows a trend wherein the the sports of `Weightlifiting` and `Basketball` have higher ratios while other sports such as `Gymnastics` and `Rhythmic Gymnastics` have lower ratios. <br>
This trend can be explained by the fact that **both the latter described sports are majorly female dominated.**

#### 8. The Top 10 Nations To Win Gold, Silver and Bronze Medals Respectively

`Top 10 Nations To Win Gold Medals`
"""

medal_df=data_df[data_df['Medal']=="Gold"].groupby(["Team"]).count().sort_values(by='ID', ascending=False).reset_index()
medal_df.head(10).style.background_gradient(cmap='Greens',axis=0)

line_colors = ["#0a2e36", "#27FB6B","#14cc60","#036d19","#09a129"]
gold_plot = px.pie(medal_df.head(10), values='ID',hole=0.6, names='Team', title='<b>Top 10 Gold Winning Nations</b>',color_discrete_sequence=line_colors,template = "none")
fig.update_layout(plot_bgcolor = " whitesmoke")
gold_plot.show()

"""`Top 10 Nations To Win Silver Medals`"""

silver_df=data_df[data_df['Medal']=="Silver"].groupby(["Team"]).count().sort_values(by='ID', ascending=False).reset_index()
silver_df.head(10).style.background_gradient(cmap='Greens',axis=0)

line_colors = ["#0a2e36", "#27FB6B","#14cc60","#036d19","#09a129"]
silver_plot = px.pie(silver_df.head(10), values='ID',hole=0.6, names='Team', title='<b>Top 10 Silver Winning Nations</b>',color_discrete_sequence=line_colors,template = "none")
silver_plot.show()

"""`Top 10 Nations To Win Bronze Medals`"""

bronze_df=data_df[data_df['Medal']=="Bronze"].groupby(["Team"]).count().sort_values(by='ID', ascending=False).reset_index()
bronze_df.head(11).style.background_gradient(cmap='Greens',axis=0)

line_colors = ["#0a2e36", "#27FB6B","#14cc60","#036d19","#09a129"]
bronze_plot = px.pie(bronze_df.head(10), values='ID',hole=0.6, names='Team', title='<b>Top 10 Bronze Winning Nations</b>',color_discrete_sequence=line_colors,template = "none")
bronze_plot.show()

"""**Inference:**
- We can clearly see that `The United States` is the undisputed leader all the three medal categories.
- The second and third positions have been captured by either `Germany` or `The Soviet Union` without any particular order.

#### 9. The Year-Wise Medal Break-up Of The Top Performing Team: `The United States`
"""

usa_medal_df=data_df[data_df['Team']=="United States"].reset_index()
usa_medal_df.head().style.background_gradient(cmap='Greens',axis=0)

yearly_medals = usa_medal_df.groupby(['Medal', 'Year']).size().reset_index().pivot(columns='Medal', index='Year', values=0).reset_index()
yearly_medals.head(10).style.set_properties(**{'background-color': '#DEF5E5',
                                    'color': 'black',
                                    'border': '0.5px  solid black'})

fig = px.bar(yearly_medals, x =  "Year", barmode = "stack", y=["Bronze", "Gold","Silver"],
            color_discrete_map={'Gold': '#14cc60', 'Silver': '#27FB6B', 'Bronze': '#036d19'})
fig.update_layout(plot_bgcolor = " whitesmoke", title = "<b>Medals-Breakup Over The Years</b>", yaxis_title = "Medal Count")

fig.show()

"""**Inference:**
- The Gold medals tally has always been more than Silver and Bronze medals.
- We can see that over the years the United States has gradually increased their share of gold medals.
- In the year 1984 they won maximum medals in a single year.

#### 10. A Word-Cloud Visualizing Sports In Which India Has Won Medals Over The Years
"""

indiasports_df= data_df[(data_df['Team']=="India")]
indiawin_df= indiasports_df[ (indiasports_df['Medal']=="Gold") | (indiasports_df['Medal']=="Silver") | (indiasports_df['Medal']=="Bronze")].sort_values(by="Medal",ascending=False)
indiawin_df.head().style.background_gradient(cmap='Greens',axis=0)

indiawin_df["Sport"].unique()

"""**Inference:**
- India seems to have won medals across countable sports over the years.
- Hockey accounts for maximum number of medals and this can be validated by the fact that it is the national sport of India.

#### 11. The Top 5 Male Athlete's By The Number Of Medals Across All Sports
"""

data_M=data_df[data_df['Sex']=='M']
topmale_df= data_M[(data_M['Medal']=="Gold")|(data_M['Medal']=="Silver")|(data_M['Medal']=="Bronze")].groupby(['Name',"Team","Sport"]).count().sort_values(by="ID",ascending=False).reset_index()
topmale_df.head().style.background_gradient(cmap='Greens',axis=0)

"""**Inference:**
- It is quite amazing that these athletes have individuallly won more medals than many countries like Indonesia, Nepal, India, and most of the African and South American Nations.
- Michael Phelps is the undisputed leader in this category with 28 medals in total.

#### 12. The Top 5 Female Athlete's By The Number Of Medals Across All Sports
"""

data_F=data_df[data_df['Sex']=='F']
topfemale_df= data_F[(data_F['Medal']=="Gold")|(data_F['Medal']=="Silver")|(data_F['Medal']=="Bronze")].groupby(['Name',"Team","Sport"]).count().sort_values(by="ID",ascending=False).reset_index()
topfemale_df.head().style.background_gradient(cmap='Greens',axis=0)

"""**Inference:**
- Similar to their male counterparts, these athletes too have individually won more medals than most of the developing nations.
- Larisa Latynina is the undisputed leader in this category with 18 medals in total.

#### 13. A Visualization Showing Sports That Have Most Number Of Events
"""

line_colors = ["#0a2e36", "#27FB6B","#14cc60","#036d19","#09a129"]
fig = px.treemap(data_df,title="<b>The Trend Of Number Of Events Under Each Sport</b>", path=[px.Constant("All Sports"), 'Sport', 'Sex', 'Medal'],color_discrete_sequence=line_colors,template = "none")
fig.update_traces(root_color="Black")
fig.update_layout(margin = dict(t=50, l=25, r=25, b=25))
fig.show()

"""**Inference:**
- From the tree-map, it is evident athletics and gymnastics followed by swimming have most number of corresponding events under them.
- Since these sports are centuries old and did not require any other sports accessories, they evolved to have a diverse set of events as a subset to original sport.
"""