Guest User

POTUS Election Data 2012-2020

a guest
Nov 8th, 2020
1,177
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/python3
  2.  
  3. import pandas as pd
  4. import requests
  5. import sys
  6.  
  7.  
  8. def collapse_results_by_party(results_by_candidate, candidates):
  9.     results_by_party = {}
  10.     for candidate, count in results_by_candidate.items(): # iteritems for Python 2
  11.         party = candidates[candidate]['party']
  12.         results_by_party[party] = results_by_party.get(party, 0) + count
  13.  
  14.     return results_by_party
  15.  
  16. states = [
  17.  'Alaska', 'Alabama', 'Arkansas', 'Arizona', 'California', 'Colorado',
  18.  'Connecticut', 'Delaware', 'Florida', 'Georgia',
  19.  'Hawaii', 'Iowa', 'Idaho', 'Illinois', 'Indiana', 'Kansas', 'Kentucky',
  20.  'Louisiana', 'Massachusetts', 'Maryland', 'Maine', 'Michigan',
  21.  'Minnesota', 'Missouri', 'Mississippi', 'Montana', 'North Carolina',
  22.  'North Dakota', 'Nebraska', 'New Hampshire', 'New Jersey', 'New Mexico',
  23.  'Nevada', 'New York', 'Ohio', 'Oklahoma', 'Oregon', 'Pennsylvania',
  24.  'Rhode Island', 'South Carolina', 'South Dakota', 'Tennessee', 'Texas',
  25.  'Utah', 'Virginia', 'Vermont', 'Washington', 'Wisconsin',
  26.  'West Virginia', 'Wyoming',
  27. ]
  28.  
  29. all_results = {}
  30. for state in states:
  31.     print('Downloading {}'.format(state))
  32.     formatted_state = state.lower().replace(' ', '-')
  33.     state_results = requests.get('https://static01.nyt.com/elections-assets/2020/data/api/2020-11-03/race-page/{}/president.json'.format(formatted_state)).json()
  34.     all_results[formatted_state] = state_results
  35.  
  36. records = []
  37. for state, state_results in all_results.items(): # iteritems() for Python 2
  38.     race = state_results['data']['races'][0]
  39.  
  40.     for candidate in race['candidates']:
  41.         if candidate['party_id'] == 'republican':
  42.             candidate['party'] = 'rep'
  43.         elif candidate['party_id'] == 'democrat':
  44.             candidate['party'] = 'dem'
  45.         else:
  46.             candidate['party'] = 'trd'
  47.     candidates = { candidate['candidate_key']: candidate for candidate in race['candidates'] }
  48.  
  49.     for data_point in race['timeseries']:
  50.         data_point['state']             = state
  51.         data_point['expected_votes']    = race['tot_exp_vote']
  52.         data_point['trump2016']         = race['trump2016']
  53.         data_point['votes2012']         = race['votes2012']
  54.         data_point['votes2016']         = race['votes2016']
  55.  
  56.         vote_shares = collapse_results_by_party(data_point['vote_shares'], candidates)
  57.         for party in ['rep', 'dem', 'trd']:
  58.             data_point['vote_share_{}'.format(party)] = vote_shares.get(party, 0)
  59.  
  60.         data_point.pop('vote_shares')
  61.         records.append(data_point)
  62.  
  63. time_series_df = pd.DataFrame.from_records(records)
  64. time_series_df.to_csv(f'nyt_ts.csv', encoding='utf-8')
  65.  
RAW Paste Data