Advertisement
Guest User

Untitled

a guest
Nov 11th, 2020
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.41 KB | None | 0 0
  1. # go to https://results.enr.clarityelections.com/MI/Oakland/105840/web.264614/#/summary
  2. # download "Detail XML" on right side of page and unzip detail.xml file
  3. # run the script below to generate some charts from DR SHINA EMAIL INVENTOR
  4.  
  5. import numpy as np
  6. import pandas as pd
  7. import seaborn
  8.  
  9. import xml.etree.ElementTree as et
  10.  
  11. xtree = et.parse("detail.xml")
  12. xroot = xtree.getroot()
  13.  
  14. contests = {}
  15. contests['straight_party'] = xroot.getchildren()[5]
  16. contests['president'] = xroot.getchildren()[6]
  17.  
  18. cast_df = pd.Series([int(x.attrib['totalVoters']) for x in xroot.getchildren()[4].getchildren()[0].getchildren()]).rename('total_voters').to_frame()
  19. cast_df['ballots'] = pd.Series([int(x.attrib['ballotsCast']) for x in xroot.getchildren()[4].getchildren()[0].getchildren()])
  20. cast_df.head()
  21.  
  22. def get_df_from_race(race_name):
  23. data_dict = {}
  24.  
  25. for choice in contests[race_name].getchildren()[:2]:
  26. choice_name = choice.attrib.get('name')
  27. pcts = choice.getchildren()
  28. # print(choice_name, [p.attrib['votes'] for p in pcts])
  29. data_dict[f'{choice_name}'] = [int(p.attrib['votes']) for p in pcts]
  30.  
  31. for choice in contests[race_name].getchildren()[2:]:
  32. choice_name = choice.attrib.get('text')
  33. for vote_type in choice.getchildren():
  34. pcts = vote_type.getchildren()
  35. # print(choice_name,vote_type.attrib['name'], [p.attrib['votes'] for p in pcts])
  36. data_dict[f'{choice_name} {vote_type.attrib["name"]}'] = [int(p.attrib['votes']) for p in pcts]
  37. return pd.DataFrame(data_dict)
  38.  
  39. straight_party = get_df_from_race('straight_party')
  40. straight_party['st_total'] = straight_party.sum(axis=1) - straight_party['Undervotes']
  41. straight_party.head()
  42.  
  43. straight_party['gop'] = straight_party['Republican Party Absentee'] + straight_party['Republican Party Election']
  44. straight_party['dem'] = straight_party['Democratic Party Absentee'] + straight_party['Democratic Party Election']
  45. straight_party.loc[:,['gop', 'dem']]
  46.  
  47. pres_df = get_df_from_race('president')
  48. pres_df['pres_total'] = pres_df.sum(axis=1)
  49.  
  50. pres_df['joe'] = pres_df['Joseph R. Biden/Kamala D. Harris Absentee'] + pres_df['Joseph R. Biden/Kamala D. Harris Election']
  51. pres_df['trump'] = pres_df['Donald J. Trump/Michael R. Pence Absentee'] + pres_df['Donald J. Trump/Michael R. Pence Election']
  52. pres_df.loc[:,["joe", "trump"]]
  53.  
  54. combo_df = pd.merge(pres_df.loc[:,["joe", "trump"]], straight_party.loc[:,['gop', 'dem', 'st_total']], left_index=True, right_index=True)
  55. combo_df = combo_df.merge(cast_df, left_index=True, right_index=True)
  56.  
  57.  
  58. norm_df = combo_df.div(combo_df.ballots, axis=0)
  59.  
  60. norm_df['trump_less_gop'] = norm_df.trump - norm_df.gop
  61.  
  62. seaborn.scatterplot(data=norm_df, x='trump_less_gop', y='gop')
  63.  
  64. norm_pres_df = pres_df.loc[:,["joe", "trump"]].div(pres_df.pres_total, axis=0)
  65. norm_sp_df = straight_party.loc[:,['gop', 'dem', 'st_total']].div(straight_party.st_total, axis=0)
  66.  
  67. norm_combo_df = pd.merge(norm_pres_df, norm_sp_df, left_index=True, right_index=True)
  68.  
  69. norm_combo_df['trump_less_gop'] = norm_combo_df.trump - norm_combo_df.gop
  70. norm_combo_df['biden_less_dem'] = norm_combo_df.joe - norm_combo_df.dem
  71. norm_combo_df['biden_less_gop'] = norm_combo_df.joe - norm_combo_df.gop
  72.  
  73. norm_combo_df.head()
  74.  
  75. seaborn.lmplot(data=norm_combo_df, x='trump', y='gop', height=8)
  76.  
  77. seaborn.lmplot(data=norm_combo_df, x='gop', y='trump_less_gop', height=8)
  78.  
  79. seaborn.lmplot(data=norm_combo_df, x='gop', y='biden_less_dem', height=8)
  80.  
  81.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement