mirror of
https://github.com/jprdonnelly/538data.git
synced 2025-12-19 17:37:43 -05:00
157 lines
3.7 KiB
Plaintext
157 lines
3.7 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 3,
|
|
"metadata": {
|
|
"collapsed": false
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"import csv \n",
|
|
"from datetime import datetime\n",
|
|
"from dateutil.relativedelta import relativedelta\n",
|
|
"\n",
|
|
"# Open the CSV file for a president\n",
|
|
"with open('obama_commutations.csv') as csvfile:\n",
|
|
" reader = csv.DictReader(csvfile)\n",
|
|
" rows = [row for row in reader]"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 4,
|
|
"metadata": {
|
|
"collapsed": false
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"rows\n",
|
|
"\n",
|
|
"# Figure out the indicies where each individual entry begins\n",
|
|
"individualSplits = []\n",
|
|
"\n",
|
|
"for i, row in enumerate(rows):\n",
|
|
" # Names don't have a key column, and the next row always has key \"Offense:\"\n",
|
|
" try:\n",
|
|
" if rows[i]['key'] == '' and (i == 0 or rows[i+1]['key'] == 'Offense:'):\n",
|
|
" individualSplits.append(i)\n",
|
|
" except:\n",
|
|
" pass"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 5,
|
|
"metadata": {
|
|
"collapsed": false
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"individuals = []\n",
|
|
"\n",
|
|
"# Parse each individual into a dict with values representing the name and the offense\n",
|
|
"for i, split in enumerate(individualSplits):\n",
|
|
" try:\n",
|
|
" nextSplit = individualSplits[i + 1]\n",
|
|
" except: \n",
|
|
" nextSplit = len(rows)\n",
|
|
" \n",
|
|
" individual = {\n",
|
|
" 'name': '',\n",
|
|
" 'offense': '',\n",
|
|
" }\n",
|
|
" \n",
|
|
" individual['name'] += rows[split]['info']\n",
|
|
" \n",
|
|
" for x in range(split + 1, nextSplit):\n",
|
|
" \n",
|
|
" if rows[x]['key'] == 'Offense:' or (rows[x]['key'] == '' and rows[x - 1]['key'] == 'Offense:'):\n",
|
|
" individual['offense'] += rows[x]['info']\n",
|
|
" \n",
|
|
" individuals.append(individual)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 6,
|
|
"metadata": {
|
|
"collapsed": false
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"drug_words = [\n",
|
|
" \"cocaine\",\n",
|
|
" \"marijuana\",\n",
|
|
" \"controlled substance\",\n",
|
|
" \"drug\",\n",
|
|
" \"distribute\",\n",
|
|
" \"distribution\",\n",
|
|
" \"heroin\",\n",
|
|
" \"LSD\",\n",
|
|
" \"manufacture\",\n",
|
|
"]\n",
|
|
"\n",
|
|
"offenses = {\n",
|
|
" 'drug': 0,\n",
|
|
" 'conspiracy': 0\n",
|
|
"}\n",
|
|
"\n",
|
|
"for person in individuals:\n",
|
|
" \n",
|
|
" crime = person['offense'].lower()\n",
|
|
" \n",
|
|
" if any(word in crime for word in drug_words):\n",
|
|
" offenses['drug'] += 1\n",
|
|
" \n",
|
|
" if \"conspiracy\" in crime:\n",
|
|
" offenses['conspiracy'] += 1\n",
|
|
"\n",
|
|
"offenses['drug'] = offenses['drug'] / len(individuals)\n",
|
|
"offenses['conspiracy'] = offenses['conspiracy'] / len(individuals)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 7,
|
|
"metadata": {
|
|
"collapsed": false
|
|
},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"{'conspiracy': 0.6227371469949312, 'drug': 0.9840695148443157}\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"print(offenses)"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {
|
|
"anaconda-cloud": {},
|
|
"kernelspec": {
|
|
"display_name": "Python [default]",
|
|
"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.5.2"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 1
|
|
}
|