#!/usr/bin/env python
# coding: utf-8
# ##Medals for participating countries at the International Olympiad in Informatics, 1989-2015##
# In[1]:
import pandas as pd
import numpy as np
# Read data from IOI [homepage](http://stats.ioinformatics.org/countries/):
# In[2]:
url=r'http://stats.ioinformatics.org/countries/'
dfs = pd.read_html(url)#returns a list of tables
df=dfs[0]
df.head()#inspect header
# Re-read skipping rows 0,1:
# In[3]:
url=r'http://stats.ioinformatics.org/countries/'
dfs = pd.read_html(url, skiprows=[0,1])
df=dfs[0]
df.head()
# In[4]:
print df.columns
# Delete columns 0, 2:
# In[5]:
df.drop([0,2], axis=1, inplace=True)
# In[6]:
print df.head()
# In[7]:
dfn=df.rename(columns = {1: 'Country', 3:'Gold', 4: 'Silver', 5: 'Bronze', 6:'Total'})
dfn.head()
# In[8]:
dfn = dfn[(dfn[['Total']] != 0).all(axis=1)]#delete countries with no medal
L= len(dfn)
dfn= dfn.set_index(np.arange(L))
dfn.head()
# List the complete table:
# In[9]:
from IPython.display import HTML
h = HTML(dfn.to_html());h
# Shorten a few country names:
# In[10]:
dfn.loc[8, 'Country']='BosniaHer'
dfn.loc[18,'Country']='CzechRep'
dfn.loc[19, 'Country']='Czechoslov'
dfn.loc[26, 'Country']='GermanDemR'
dfn.loc[29,'Country']='HongKong'
dfn.loc[51,'Country']='NewZealand'
dfn.loc[56, 'Country']='RepKorea'
dfn.loc[60, 'Country']='SerbiaMoN'
dfn.loc[64, 'Country']='SouthAfrica'
dfn.loc[65, 'Country']='SovUnion'
dfn.loc[67, 'Country']='SriLanka'
dfn.loc[74, 'Country']='TrinidadTo'
dfn.loc[79, 'Country']='UnKingdom'
dfn.loc[80, 'Country']='USA'
# In[11]:
#Check data again
#h = HTML(dfn.to_html());h
# Convert data from the last 4 columns to int:
# In[12]:
for c in ['Gold', 'Silver', 'Bronze', 'Total']:
dfn[c]=dfn[c].astype(int)
# Sort data increasingly with respect to the total number of medals:
# In[13]:
dfs=dfn.sort(columns='Total')
L=len(dfs)
dfs= dfs.set_index(np.arange(L))
dfs.head()
# Set colors for Gold, Silver and Bronze medal:
# In[14]:
colors=['#D9D919', '#C0C0C0', '#A67D3D'] #bright gold, silver, bronze
# In[15]:
import plotly.plotly as py
from plotly.graph_objs import *
py.sign_in("empet", "shzbvu3hbh")
# In[16]:
Co=dfs['Country'].values.tolist()
Go=dfs['Gold'].values.tolist()
Si=dfs['Silver'].values.tolist()
Bro=dfs['Bronze'].values.tolist()
To=dfs['Total'].values.tolist()
# In[17]:
print max(To)
# In[18]:
anno_text="Source: [1]"
trace1= Bar(
x=Go,
y=Co,
orientation='h',
marker=Marker(color=colors[0]),
name='gold'
)
trace2= Bar(
x=Si,
y=Co,
orientation='h',
marker=Marker(color=colors[1]),
name='silver'
)
trace3= Bar(
x=Bro,
y=Co,
orientation='h',
marker=Marker(color=colors[2]),
name='bronze'
)
data = Data([trace1, trace2, trace3])
m_title='Unofficial country rankings according to the total number of medals at IOI
1989-2015'
layout = Layout(
title=m_title,
font=Font(family='Raleway, sans-serif'
),
showlegend=False,
autosize=False,
width=800,
height=1000,
xaxis=XAxis(
range=[0, 108],
gridwidth=2,
showgrid=True,
zeroline=True,
showline=True,
mirror=True,
side='top'
),
yaxis=YAxis(
#title='Country',
showgrid=False,
zeroline=True,
showline=True,
autotick=True,
nticks=0,
ticks='outside',
showticklabels=True,
tick0=0,
dtick=1,
ticklen=10,
mirror=True
),
margin=Margin(
l=80,
r=80,
b=80,
t=100,
pad=2
),
annotations=Annotations([
Annotation(
showarrow=False,
text=anno_text,
xref='paper',
yref='paper',
x=0,
y=-0.075,
xanchor='left',
yanchor='bottom',
font=Font(
size=14
)
)
]),
bargap=0.18,
bargroupgap=0,
hovermode='y',
dragmode='zoom',
barmode='stack',
)
fig = Figure(data=data, layout=layout)
py.iplot(fig, filename='IOI-Rankings', height=1005)
# In[44]:
from IPython.core.display import HTML
def css_styling():
styles = open("./custom.css", "r").read()
return HTML(styles)
css_styling()