!pip install openpyxl

import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)
import seaborn as sns
import matplotlib.pyplot as plt


import os
for dirname, _, filenames in os.walk('/kaggle/input'):
    for filename in filenames:
        print(os.path.join(dirname, filename))
"""
## Expolority Data Analysis
"""
athlete_events = pd.read_excel("../input/olympics-data/Olympics Dataset.xlsx", sheet_name = 0)
contry_def = pd.read_excel("../input/olympics-data/Olympics Dataset.xlsx", sheet_name = 1)
"""
### Dataframe Summary
"""
print("##################### Shape #####################")
print(athlete_events.shape)

print("##################### Types #####################")
print(athlete_events.dtypes)

print("##################### Head #####################")
print(athlete_events.head())

print("##################### Tail #####################")
print(athlete_events.tail())

print("##################### NA #####################")
print(athlete_events.isnull().sum())

print("##################### Summary #####################")
print(athlete_events.describe().T)
"""
### Analysis of Categorical Variables
"""
"""
Let's find the which columns are categoric;
"""
categoric_columns = [col for col in athlete_events.columns if athlete_events[col].dtypes =="O" ]
print(categoric_columns)
def categoric_summary(dataframe, col_name, plot=False):
    print(pd.DataFrame({ col_name : dataframe[col_name].value_counts()}))
    
    if plot:
        sns.countplot(x=dataframe[col_name], 
                      data=dataframe,
                      palette="Greens_d")
        plt.title(str(col_name))
categoric_summary(athlete_events,
                 "Sex",
                 plot=True)
categoric_summary(athlete_events,
                 "Medal",
                 plot=True)
athlete_events["Age"].min()
athlete_events["Age"].max()

athlete_events["Age_Cat"] = pd.cut(athlete_events["Age"], bins = [0, 18, 25, 35, 45, 55, 65, 75, 85, 97],
                                                          labels = ["0-18", "19-25", "26-35", "36-45", "46-55", "56-65", "66-75", "76-85", "86-97"])
categoric_summary(athlete_events,
                 "Age_Cat",
                 plot=True)
df = pd.DataFrame(athlete_events["Games"].value_counts())
df.reset_index(inplace = True)
df.sort_values("index", ascending=False, inplace=True)
df.head()
df = athlete_events.groupby(["Year","Season"]).agg({"ID": "count"}).sort_values("ID", ascending=False)
df.reset_index(inplace = True)
df.head()
new_df = pd.DataFrame([str(row[0])+"_"+str(row[1].upper()) for row in df.values])
new_df["count"] = df.ID
new_df = new_df.head(10)
figure, axis = plt.subplots()
plt.xticks(rotation=90)
plot = axis.bar(new_df[0], new_df["count"] )

for rectangle in plot:
    height = rectangle.get_height()
    axis.text(rectangle.get_x() + rectangle.get_width() /2., height, '%d' % int(height), 
              ha='center', va = 'bottom')
    
plt.show()