Last active
January 25, 2024 19:27
-
-
Save BurgosNY/aaafcdbdf3391ae0641df25dac02f7fe to your computer and use it in GitHub Desktop.
Código para descobrir como foi a votação (excluindo presidente) em determinada seção eleitoral
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"id": "31797db4", | |
"metadata": {}, | |
"source": [ | |
"# Explorando dados do TSE com Python \n", | |
"\n", | |
"Para rodar o código, você precisa ter o Python 3.5+ instalado na sua máquina (veja como fazer isso [aqui](https://www.python.org/downloads/)) e instalar a biblioteca [Pandas](https://pandas.pydata.org/), além do [Jupyter Notebook](https://jupyter.org/). " | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "c44ae369", | |
"metadata": {}, | |
"source": [ | |
"1. Obtendo os dados. Escolha o seu estado e baixe os [dados do TSE](https://dadosabertos.tse.jus.br/dataset/resultados-2022). Descompacte o zip e coloque, de preferência, no mesmo diretório deste notebook\n", | |
"\n", | |
"2. Sugiro começar com um estado menos populoso, como Acre, para testar o código. " | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"id": "3a2f3fd9", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# Importando a biblioteca\n", | |
"import pandas as pd\n", | |
"\n", | |
"# Coloque o nome do arquivo que você quer abrir aqui. No exemplo, o csv de SP\n", | |
"arquivo = 'votacao_secao_2022_SP.csv'\n", | |
"\n", | |
"# Crie um dataframe com votos de cada seção do estado. Isso pode levar alguns minutos.\n", | |
"sp = pd.read_csv(arquivo, sep=\";\", encoding=\"latin-1\")" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"id": "f758090d", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# Criando uma função que usa como parâmetros o município, zona e seção para saber os mais votados\n", | |
"def leaderboard_secao(municipio, zona, secao, uf_df):\n", | |
" cargos = uf_df['DS_CARGO'].unique()\n", | |
" data = uf_df.query(f\"NM_MUNICIPIO == '{municipio}' & NR_ZONA == {zona} & NR_SECAO == {secao}\").copy()\n", | |
" obj = {}\n", | |
" for c in cargos:\n", | |
" new_df = data.query(f\"DS_CARGO == '{c}'\").copy()[['NM_VOTAVEL', 'QT_VOTOS']].sort_values('QT_VOTOS', ascending=False)\n", | |
" votos = new_df['QT_VOTOS'].sum()\n", | |
" new_df['PROP'] = round((new_df['QT_VOTOS']/votos)*100, 1)\n", | |
" obj[c] = new_df\n", | |
" return obj " | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"id": "f53bafc7", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# Agora rodamos a função, usando como parâmetros, além de zona e seção,\n", | |
"# \"sp\", que é o dataframe gerado com os resultado. \n", | |
"# Armazenamos o resultado na variável resultados \n", | |
"resultados = leaderboard_secao('SÃO PAULO', 1, 8, sp)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"id": "fa02e041", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"dict_keys(['GOVERNADOR', 'SENADOR', 'DEPUTADO FEDERAL', 'DEPUTADO ESTADUAL'])" | |
] | |
}, | |
"execution_count": 4, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# Resultados é um \"dicionário\" com 4 resultados. \n", | |
"resultados.keys()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"id": "705fa556", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/html": [ | |
"<div>\n", | |
"<style scoped>\n", | |
" .dataframe tbody tr th:only-of-type {\n", | |
" vertical-align: middle;\n", | |
" }\n", | |
"\n", | |
" .dataframe tbody tr th {\n", | |
" vertical-align: top;\n", | |
" }\n", | |
"\n", | |
" .dataframe thead th {\n", | |
" text-align: right;\n", | |
" }\n", | |
"</style>\n", | |
"<table border=\"1\" class=\"dataframe\">\n", | |
" <thead>\n", | |
" <tr style=\"text-align: right;\">\n", | |
" <th></th>\n", | |
" <th>NM_VOTAVEL</th>\n", | |
" <th>QT_VOTOS</th>\n", | |
" <th>PROP</th>\n", | |
" </tr>\n", | |
" </thead>\n", | |
" <tbody>\n", | |
" <tr>\n", | |
" <th>671107</th>\n", | |
" <td>FERNANDO HADDAD</td>\n", | |
" <td>91</td>\n", | |
" <td>42.1</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>282904</th>\n", | |
" <td>TARCISIO GOMES DE FREITAS</td>\n", | |
" <td>73</td>\n", | |
" <td>33.8</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>593137</th>\n", | |
" <td>RODRIGO GARCIA</td>\n", | |
" <td>39</td>\n", | |
" <td>18.1</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>384110</th>\n", | |
" <td>VINICIUS LAZZER POIT</td>\n", | |
" <td>6</td>\n", | |
" <td>2.8</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>166889</th>\n", | |
" <td>VOTO BRANCO</td>\n", | |
" <td>4</td>\n", | |
" <td>1.9</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>49701</th>\n", | |
" <td>VOTO NULO</td>\n", | |
" <td>2</td>\n", | |
" <td>0.9</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>464715</th>\n", | |
" <td>ALTINO DE MELO PRAZERES JUNIOR</td>\n", | |
" <td>1</td>\n", | |
" <td>0.5</td>\n", | |
" </tr>\n", | |
" </tbody>\n", | |
"</table>\n", | |
"</div>" | |
], | |
"text/plain": [ | |
" NM_VOTAVEL QT_VOTOS PROP\n", | |
"671107 FERNANDO HADDAD 91 42.1\n", | |
"282904 TARCISIO GOMES DE FREITAS 73 33.8\n", | |
"593137 RODRIGO GARCIA 39 18.1\n", | |
"384110 VINICIUS LAZZER POIT 6 2.8\n", | |
"166889 VOTO BRANCO 4 1.9\n", | |
"49701 VOTO NULO 2 0.9\n", | |
"464715 ALTINO DE MELO PRAZERES JUNIOR 1 0.5" | |
] | |
}, | |
"execution_count": 5, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# Para acessar os resultados de 'GOVERNADOR' de uma determinada seção basta fazer:\n", | |
"resultados['GOVERNADOR']" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "cebd1377", | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3 (ipykernel)", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.10.6" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 5 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment