<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Map election results to regions.
Assumes you have huffpostdata/election-2012-results cloned at ../../election-2012-results. Does 2000 regions by default; just change county_region_00 to county_region_10 below to do 2010.
Step1:  Map electoral results to regions
Step2:  First, handle Alaska specially
Step3:  Normalize candidate names
Step4:  Slightly disagrees with https
Step5:  UOCAVA = The Uniformed and Overseas Citizens Absentee Voting Act. Ignore these.
Step6:  15005 is Kalawao County, Hawaii, which has a population of 89 and is accessible only by mule trail. Its votes are counted under Maui (15009), and they're in the same PUMA anyway
Step7:  Do the actual grouping
<ASSISTANT_TASK:>
Python Code:
from __future__ import division, print_function
%matplotlib inline
import numpy as np
import pandas as pd
import re
import six
from IPython.display import display
import sys
sys.path.append('..')
from pummeler.data import geocode_data
county_to_region = geocode_data('county_region_10').region.to_dict()
Explanation: Map election results to regions.
Assumes you have huffpostdata/election-2012-results cloned at ../../election-2012-results. Does 2000 regions by default; just change county_region_00 to county_region_10 below to do 2010.
End of explanation
from glob import glob
Explanation: Map electoral results to regions
End of explanation
assert len({v for k, v in county_to_region.iteritems() if k.startswith('02')}) == 1
ak_precincts = pd.read_csv('../../election-2012-results/data/ak_precincts.csv')
ak = ak_precincts.groupby(ak_precincts.candidate).sum().reset_index()
ak['state'] = 'ak'
ak['fips'] = next(k for k in county_to_region if k.startswith('02'))
ak['county'] = 'All of Alaska'
ak
bits = [ak]
for f in glob('../../election-2012-results/data/??.csv'):
    piece = pd.read_csv(f, dtype={'fips': str})
    piece['state'] = f[-6:-4]
    bits.append(piece)
election = pd.concat(bits)
Explanation: First, handle Alaska specially:
End of explanation
reps = {
    'goode': 'virgil goode',
    'obama': 'barack obama',
    'johnson': 'gary johnson',
    'romney': 'mitt romney',
    'stein': 'jill stein',
    'virgil h. goode': 'virgil goode',
    'virgil h. goode jr.': 'virgil goode',
    'gary e. johnson': 'gary johnson',
    'write in': 'write-in',
    'write-ins': 'write-in',
    'hoefling': 'tom hoefling',
    'obama barack': 'barack obama',
    'stein jill': 'jill stein',
    'romney mitt': 'mitt romney',
    'johnson gary': 'gary johnson',
    'jill stein write-in': 'jill stein',
    'hoefling (write-in)': 'tom hoefling',
    'tom hoeffling': 'tom hoefling',
    'alexander': 'stewart alexander',
    'ross c. &quot;rocky&quot;': 'ross c. "rocky"',
    'ross c. rocky': 'ross c. "rocky"',
    'ross c.': 'ross c. "rocky"',
    'rocky': 'ross c. "rocky"',
    'paul': 'ron paul',
    'ron paul write-in': 'ron paul',
    'write-in**': 'write-in',
    'clymer': 'james clymer',
    'roth': 'cecil james roth',
    'prokopich': 'barbara prokopich',
    'barbara a. prokopich': 'barbara prokopich',
    'kevin m. thorne': 'kevin thorne',
    'thorne': 'kevin thorne',
}
def rewrite(s):
    s = s.lower()
    for x in ['/', ',', '(', ' and', ' for president']:
        p = s.find(x)
        if p != -1:
            s = s[:p]
    s = s.strip().replace('  ', ' ')
    s = reps.get(s, s)
    return s
election['cand'] = election.candidate.apply(rewrite)
cand_votes = election.groupby(election.cand).votes.sum().sort_values(ascending=False)
cand_votes.head(50)
election['party'] = 'oth'
election.loc[election.cand == 'barack obama', 'party'] = 'D'
election.loc[election.cand == 'mitt romney', 'party'] = 'R'
election.loc[election.cand == 'gary johnson', 'party'] = 'L'
election.loc[election.cand == 'jill stein', 'party'] = 'G'
election.groupby(election.party).votes.sum()
Explanation: Normalize candidate names
End of explanation
set(election.fips) - set(county_to_region)
election[pd.isnull(election.fips)]
Explanation: Slightly disagrees with https://en.wikipedia.org/wiki/United_States_presidential_election,_2012: they say Obama 65,915,795, Romney 60,933,504. Not sure how we got too many votes for Romney there; maybe Wikipedia miscounted?
Make sure that the FIPS codes are lining up reasonably
End of explanation
{fips for fips in set(county_to_region) - set(election.fips)
 if not fips.startswith('02')}
Explanation: UOCAVA = The Uniformed and Overseas Citizens Absentee Voting Act. Ignore these.
End of explanation
county_to_region['15005'] == county_to_region['15009']
Explanation: 15005 is Kalawao County, Hawaii, which has a population of 89 and is accessible only by mule trail. Its votes are counted under Maui (15009), and they're in the same PUMA anyway:
End of explanation
election_region = election.groupby(election.fips.map(county_to_region)) \
                          .apply(lambda x: x.votes.groupby(x.party).sum()).unstack()
election_region.index.name = 'region'
election_region.columns = ['votes_{}'.format(p) for p in election_region.columns]
election_region.fillna(0, inplace=True)
election_region = election_region.astype('int')
election_region.head()
election_region.to_csv('2012-by-region.csv.gz', compression='gzip')
Explanation: Do the actual grouping
End of explanation
<END_TASK>