Data of the elections of Spain in 2015 and 2106
the data is from http://elecciones.elperiodico.com/resultados/generales/2016/espana/
In [1]:
import pandas
In [2]:
raw_elections_2015 = pandas.read_csv("test2015.csv",delimiter='\t')
raw_elections_2016 = pandas.read_csv("test2016.csv",delimiter='\t')
In [3]:
def find_PODEMOS(words):
if 'PODEMOS' in words:
return 'PODEMOS'
if 'EN COMU' == words:
return 'PODEMOS'
return words
elections_2015 = raw_elections_2015
elections_2016 = raw_elections_2016
elections_2015['Partido'] = elections_2015['Partido'].apply(find_PODEMOS)
elections_2016['Partido'] = elections_2016['Partido'].apply(find_PODEMOS)
print(elections_2015.columns)
In [4]:
#The data is in spanish format (10,0 and 1.000)
def replace_dots(word):
return word.replace('.','')
def replace_commas(word):
return word.replace(',','.')
elections_2015['Votos'] = elections_2015['Votos'].apply(replace_dots)
elections_2015['%'] = elections_2015['%'].apply(replace_commas)
elections_2016['Votos'] = elections_2016['Votos'].apply(replace_dots)
elections_2016['%'] = elections_2016['%'].apply(replace_commas)
In [5]:
elections_2015.info()
All the data are string
In [6]:
elections_2015['Votos'] = pandas.to_numeric(elections_2015['Votos'], errors='coerse')
elections_2015['Escannos'] = pandas.to_numeric(elections_2015['Escannos'], errors='coerse')
elections_2015['%'] = pandas.to_numeric(elections_2015['%'], errors='coerse')
elections_2016['Votos'] = pandas.to_numeric(elections_2016['Votos'], errors='coerse')
elections_2016['Escannos'] = pandas.to_numeric(elections_2016['Escannos'], errors='coerse')
elections_2016['%'] = pandas.to_numeric(elections_2016['%'], errors='coerse')
print(elections_2015.head())
print(elections_2016.head())
In [7]:
elections_2015.describe()
Out[7]:
In [8]:
elections_2016 = elections_2016.groupby('Partido',as_index=False).sum()
elections_2015 = elections_2015.groupby('Partido',as_index=False).sum()
In [9]:
elections_2015.columns=['Partido','Votos 2015','% 2015', 'Escanos 2015']
elections_2016.columns=['Partido','Votos 2016','% 2016', 'Escanos 2016']
In [10]:
belections = pandas.merge(elections_2015, elections_2016, on='Partido',how='outer')
elections = pandas.merge(elections_2015, elections_2016, on='Partido',how='inner')
In [11]:
elections.sort_values('Votos 2015', ascending = False)
Out[11]:
In [12]:
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline
sns.set_style("whitegrid")
sns.set_style({"grid.color": "1."})
sns.set_context("notebook", font_scale=1.5)
In [15]:
election_filtred = elections[elections['% 2015'] > 1.01]
election_filtred = election_filtred.sort_values('% 2016', ascending=False)
columns = election_filtred['Partido']
#Dataframe transposition for graph
dict_pre = {}
for i in range(election_filtred.shape[0]):
#print(i)
dict_pre[election_filtred.iloc[i,0]] = [election_filtred.iloc[i,1],election_filtred.iloc[i,4]]
election_graph = pandas.DataFrame(dict_pre, index=[2015,2016])
election_graph.columns = columns
election_graph.plot(colormap='Paired')
Out[15]:
In [ ]:
#elections.to_csv("elections_clean.csv")
In [ ]: