Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # go to https://results.enr.clarityelections.com/MI/Oakland/105840/web.264614/#/summary
- # download "Detail XML" on right side of page and unzip detail.xml file
- # run the script below to generate some charts from DR SHINA EMAIL INVENTOR
- import numpy as np
- import pandas as pd
- import seaborn
- import xml.etree.ElementTree as et
- xtree = et.parse("detail.xml")
- xroot = xtree.getroot()
- contests = {}
- contests['straight_party'] = xroot.getchildren()[5]
- contests['president'] = xroot.getchildren()[6]
- cast_df = pd.Series([int(x.attrib['totalVoters']) for x in xroot.getchildren()[4].getchildren()[0].getchildren()]).rename('total_voters').to_frame()
- cast_df['ballots'] = pd.Series([int(x.attrib['ballotsCast']) for x in xroot.getchildren()[4].getchildren()[0].getchildren()])
- cast_df.head()
- def get_df_from_race(race_name):
- data_dict = {}
- for choice in contests[race_name].getchildren()[:2]:
- choice_name = choice.attrib.get('name')
- pcts = choice.getchildren()
- # print(choice_name, [p.attrib['votes'] for p in pcts])
- data_dict[f'{choice_name}'] = [int(p.attrib['votes']) for p in pcts]
- for choice in contests[race_name].getchildren()[2:]:
- choice_name = choice.attrib.get('text')
- for vote_type in choice.getchildren():
- pcts = vote_type.getchildren()
- # print(choice_name,vote_type.attrib['name'], [p.attrib['votes'] for p in pcts])
- data_dict[f'{choice_name} {vote_type.attrib["name"]}'] = [int(p.attrib['votes']) for p in pcts]
- return pd.DataFrame(data_dict)
- straight_party = get_df_from_race('straight_party')
- straight_party['st_total'] = straight_party.sum(axis=1) - straight_party['Undervotes']
- straight_party.head()
- straight_party['gop'] = straight_party['Republican Party Absentee'] + straight_party['Republican Party Election']
- straight_party['dem'] = straight_party['Democratic Party Absentee'] + straight_party['Democratic Party Election']
- straight_party.loc[:,['gop', 'dem']]
- pres_df = get_df_from_race('president')
- pres_df['pres_total'] = pres_df.sum(axis=1)
- pres_df['joe'] = pres_df['Joseph R. Biden/Kamala D. Harris Absentee'] + pres_df['Joseph R. Biden/Kamala D. Harris Election']
- pres_df['trump'] = pres_df['Donald J. Trump/Michael R. Pence Absentee'] + pres_df['Donald J. Trump/Michael R. Pence Election']
- pres_df.loc[:,["joe", "trump"]]
- combo_df = pd.merge(pres_df.loc[:,["joe", "trump"]], straight_party.loc[:,['gop', 'dem', 'st_total']], left_index=True, right_index=True)
- combo_df = combo_df.merge(cast_df, left_index=True, right_index=True)
- norm_df = combo_df.div(combo_df.ballots, axis=0)
- norm_df['trump_less_gop'] = norm_df.trump - norm_df.gop
- seaborn.scatterplot(data=norm_df, x='trump_less_gop', y='gop')
- norm_pres_df = pres_df.loc[:,["joe", "trump"]].div(pres_df.pres_total, axis=0)
- norm_sp_df = straight_party.loc[:,['gop', 'dem', 'st_total']].div(straight_party.st_total, axis=0)
- norm_combo_df = pd.merge(norm_pres_df, norm_sp_df, left_index=True, right_index=True)
- norm_combo_df['trump_less_gop'] = norm_combo_df.trump - norm_combo_df.gop
- norm_combo_df['biden_less_dem'] = norm_combo_df.joe - norm_combo_df.dem
- norm_combo_df['biden_less_gop'] = norm_combo_df.joe - norm_combo_df.gop
- norm_combo_df.head()
- seaborn.lmplot(data=norm_combo_df, x='trump', y='gop', height=8)
- seaborn.lmplot(data=norm_combo_df, x='gop', y='trump_less_gop', height=8)
- seaborn.lmplot(data=norm_combo_df, x='gop', y='biden_less_dem', height=8)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement