Last active
February 26, 2020 01:16
-
-
Save tewson/667b87c565423e5f2543dc5b92fe2b27 to your computer and use it in GitHub Desktop.
Rank TDs by number of votes participated in 2019 Using the Houses of the Oireachtas Open Data APIs
This file contains hidden or 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
| name: rank-td-by-dail-votes-2019 | |
| channels: | |
| - conda-forge | |
| dependencies: | |
| - python | |
| - pip | |
| - pip: | |
| - pandas | |
| - matplotlib |
This file contains hidden or 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", | |
| "metadata": {}, | |
| "source": [ | |
| "# Rank TDs by number of votes participated in 2019\n", | |
| "## Using the Houses of the Oireachtas Open Data APIs" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "def sort_tds_by_vote_count(all_dail_votes):\n", | |
| " vote_types = ['nilVotes', 'staonVotes', 'taVotes']\n", | |
| " tds_votes_dict = {}\n", | |
| " \n", | |
| " for result in all_dail_votes:\n", | |
| " for vote_type in vote_types:\n", | |
| " for memberWrapper in result['division']['tallies'][vote_type]['members']:\n", | |
| " member = memberWrapper['member']\n", | |
| " memberCode = member['memberCode']\n", | |
| " if memberCode in tds_votes_dict:\n", | |
| " tds_votes_dict[memberCode]['voteCount'] += 1\n", | |
| " else:\n", | |
| " tds_votes_dict[memberCode] = {\n", | |
| " 'name': member['showAs'],\n", | |
| " 'voteCount': 1\n", | |
| " }\n", | |
| "\n", | |
| " return sorted(\n", | |
| " tds_votes_dict.values(),\n", | |
| " key = lambda entry: entry['voteCount'],\n", | |
| " reverse=True)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "import requests\n", | |
| "import json\n", | |
| "import pandas as pd\n", | |
| "import matplotlib.pyplot as plt\n", | |
| "%matplotlib inline\n", | |
| "\n", | |
| "params = {\n", | |
| " 'chamber_type': 'house',\n", | |
| " 'chamber_id': 'https://data.oireachtas.ie/ie/oireachtas/house/dail/32',\n", | |
| " 'chamber': 'dail',\n", | |
| " 'date_start': '2019-01-01',\n", | |
| " 'date_end': '2019-12-31',\n", | |
| " 'limit': '10000'\n", | |
| "}\n", | |
| "r = requests.get('https://api.oireachtas.ie/v1/divisions', params=params)\n", | |
| "all_dail_votes = r.json()['results']\n", | |
| "\n", | |
| "sorted_tds_by_vote_count = sort_tds_by_vote_count(all_dail_votes)\n", | |
| " \n", | |
| "df = pd.DataFrame(sorted_tds_by_vote_count)\n", | |
| "df.set_index(\"name\",drop=True,inplace=True) \\\n", | |
| "\n", | |
| "df.sort_values(by='voteCount', ascending=True) \\\n", | |
| " .plot.barh(figsize=(10, 80))" | |
| ] | |
| } | |
| ], | |
| "metadata": { | |
| "kernelspec": { | |
| "display_name": "Python 3", | |
| "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.7.5" | |
| } | |
| }, | |
| "nbformat": 4, | |
| "nbformat_minor": 4 | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment