Guest User

Untitled

a guest
Nov 7th, 2020
1,195
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import itertools
  2. import matplotlib.pyplot as plt
  3. import pandas as pd
  4. import pprint
  5. import requests
  6.  
  7. presidential_json = requests.get('https://static01.nyt.com/elections-assets/2020/data/api/2020-11-03/national-map-page/national/president.json').json()
  8. senate_json = requests.get('https://static01.nyt.com/elections-assets/2020/data/api/2020-11-03/national-map-page/national/senate.json').json()
  9.  
  10. def collapse_results_by_party(results_by_candidate, candidates):
  11. results_by_party = {}
  12. for candidate, count in results_by_candidate.iteritems():
  13. party = candidates[candidate]['party']
  14. results_by_party[party] = results_by_party.get(party, 0) + count
  15.  
  16. return results_by_party
  17.  
  18. def race_json_to_party_county_results(race_json):
  19. county_results = []
  20. for r in race_json['data']['races']:
  21. for candidate in r['candidates']:
  22. if candidate['party_id'] == 'republican':
  23. candidate['party'] = 'rep'
  24. elif candidate['party_id'] == 'democrat':
  25. candidate['party'] = 'dem'
  26. else:
  27. candidate['party'] = 'trd'
  28. candidates = { candidate['candidate_key']: candidate for candidate in r['candidates'] }
  29.  
  30. for c in r['counties']:
  31. c['state'] = r['state_name']
  32. c['race_id'] = r['race_id']
  33. results = collapse_results_by_party(c['results'], candidates)
  34. absentee_results = collapse_results_by_party(c['results_absentee'], candidates)
  35. for party in ['rep', 'dem', 'trd']:
  36. c['had_{}'.format(party)] = party in results
  37. c['results_{}'.format(party)] = results.get(party, 0)
  38. c['absentee_results_{}'.format(party)] = absentee_results.get(party, 0)
  39.  
  40. c.pop('results_absentee')
  41. c.pop('results')
  42. county_results.append(c)
  43. return county_results
  44.  
  45. pres_county_results = race_json_to_party_county_results(presidential_json)
  46. senate_county_results = race_json_to_party_county_results(senate_json)
  47.  
  48. pres_results_df = pd.DataFrame.from_records(pres_county_results)
  49. pres_results_df['rep_share'] = pres_results_df['results_rep'] / pres_results_df['votes']
  50. pres_results_df['dem_share'] = pres_results_df['results_dem'] / pres_results_df['votes']
  51. pres_results_df['trd_share'] = pres_results_df['results_trd'] / pres_results_df['votes']
  52.  
  53. senate_results_df = pd.DataFrame.from_records(senate_county_results)
  54. senate_results_df['rep_share'] = senate_results_df['results_rep'] / senate_results_df['votes']
  55. senate_results_df['dem_share'] = senate_results_df['results_dem'] / senate_results_df['votes']
  56. senate_results_df['trd_share'] = senate_results_df['results_trd'] / senate_results_df['votes']
  57.  
  58. joint_results_df = pres_results_df.merge(senate_results_df, on=['name', 'state'], suffixes=['_pres', '_sen'], how='outer').reset_index()
  59.  
  60. joint_results_df.to_csv('data/joint_results.csv', encoding='utf-8')
RAW Paste Data