Package: elections
Easy access to (US 2020, for now) election statistics.How to useimportpandasaspdfromelectionsimportPresident2020TimeSeries,Races2020,Election2020RawJsonPresident's race statss=President2020TimeSeries()len(s)51sis a dictionary-like interface to the presidential race.It's keys are the statesprint(*s)alabama alaska arizona arkansas california colorado connecticut delaware district-of-columbia florida georgia hawaii idaho illinois indiana iowa kansas kentucky louisiana maine maryland massachusetts michigan minnesota mississippi missouri montana nebraska nevada new-hampshire new-jersey new-mexico new-york north-carolina north-dakota ohio oklahoma oregon pennsylvania rhode-island south-carolina south-dakota tennessee texas utah vermont virginia washington west-virginia wisconsin wyomingIt's values are dataframes containing the stats.state='georgia'df=s[state]df<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }.dataframe tbody tr th {
    vertical-align: top;
}

.dataframe thead th {
    text-align: right;
}</style>voteseevpeevp_sourcetrumpdbidenjtimestamp2020-11-04T09:23:03Z00edison0.0000.0002020-11-04T00:14:11Z4080edison0.6740.3262020-11-04T00:15:51Z1271062edison0.3700.6182020-11-04T00:19:55Z1736383edison0.4310.5572020-11-04T00:21:57Z1740063edison0.4320.557..................2020-11-06T23:14:40Z496986099edison0.4930.4942020-11-06T23:17:43Z496986999edison0.4930.4942020-11-06T23:36:39Z496987399edison0.4930.4942020-11-06T23:41:44Z496988099edison0.4930.4942020-11-06T23:45:40Z497009399edison0.4930.494456 rows × 5 columnsdf['bidenj'].plot(figsize=(16,6),grid=True,title=state);Other racesBut that's not the only race going on here.s=Races2020()len(s)51data=s['new-york']# by the way, you can tab-complete this if you're working in a jupyter notebookprint(type(data))print(f"{len(data)}items... Here are the first 5:")list(data)[:5]<class 'py2store.base.Store'>
242 items... Here are the first 5:





['president-general-2020-11-03',
 'house-general-district-001-2020-11-03',
 'house-general-district-002-2020-11-03',
 'house-general-district-003-2020-11-03',
 'house-general-district-004-2020-11-03']So we see that now, instead of just getting the president's race, we get... 242 races (one of which is the president's race).What you need to know, as well, is thatPresident2020TimeSeriesjust gave you one of the many datas available for the race (the 'timeseries' one), extracted and formated for your convenience, since it's probably the main information you're here for.But there are other associated (raw) datas you may or may not be interested in. Here's what you got:data['president-general-2020-11-03'].keys()# you can tab complete here as well (you're welcome!)dict_keys(['race_id', 'race_slug', 'url', 'state_page_url', 'ap_polls_page', 'edison_exit_polls_page', 'race_type', 'election_type', 'election_date', 'runoff', 'race_name', 'office', 'officeid', 'race_rating', 'seat', 'seat_name', 'state_id', 'state_slug', 'state_name', 'state_nyt_abbrev', 'state_shape', 'party_id', 'uncontested', 'report', 'result', 'result_source', 'gain', 'lost_seat', 'votes', 'electoral_votes', 'absentee_votes', 'absentee_counties', 'absentee_count_progress', 'absentee_outstanding', 'absentee_max_ballots', 'provisional_outstanding', 'provisional_count_progress', 'poll_display', 'poll_countdown_display', 'poll_waiting_display', 'poll_time', 'poll_time_short', 'precincts_reporting', 'precincts_total', 'reporting_display', 'reporting_value', 'eevp', 'tot_exp_vote', 'eevp_source', 'eevp_value', 'eevp_display', 'county_data_source', 'incumbent_party', 'no_forecast', 'last_updated', 'candidates', 'has_incumbent', 'leader_margin_value', 'leader_margin_votes', 'leader_margin_display', 'leader_margin_name_display', 'leader_party_id', 'counties', 'votes2016', 'margin2016', 'clinton2016', 'trump2016', 'votes2012', 'margin2012', 'expectations_text', 'expectations_text_short', 'absentee_ballot_deadline', 'absentee_postmark_deadline', 'update_sentences', 'race_diff', 'winnerCalledTimestamp', 'timeseries'])t=data['president-general-2020-11-03']print(t['trump2016'],t['clinton2016'])2819534 4556124Election2020RawJsonBut if you want even more, and even more raw than the above, we can give that to you.WithElection2020RawJsonyou get access to the original full json.raw_jsons=Election2020RawJson()json_data=raw_jsons['california']json_data.keys()dict_keys(['data', 'meta'])json_data['meta']{'version': 10403,
 'track': '2020-11-03',
 'timestamp': '2020-11-06T23:52:57.623Z'}json_data['data'].keys()dict_keys(['races', 'party_control', 'liveUpdates'])pd.DataFrame(json_data['data']['party_control']).set_index('race_type').T<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }.dataframe tbody tr th {
    vertical-align: top;
}

.dataframe thead th {
    text-align: right;
}</style>race_typehousepresidentsenatestate_idneeded_for_control21827050total435538100no_election{}{}{'democrat': 35, 'republican': 30, 'other': 0}winnerFalseFalseFalseparties{'democrat': {'party_id': 'democrat', 'name_di...{'democrat': {'party_id': 'democrat', 'name_di...{'democrat': {'party_id': 'democrat', 'name_di...winnerCalledTimestampNoneNoneNonepd.DataFrame(json_data['data']['liveUpdates'])<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }.dataframe tbody tr th {
    vertical-align: top;
}

.dataframe thead th {
    text-align: right;
}</style>idauthorauthor_title_or_locationtextlink_urllink_textlinked_state_1linked_state_2linked_state_3image_url...call_typerace_idwinnerparty_idcandidate_last_namecandidate_name_displaycandidate_idrace_call_party_winnerstate_namelink0333Nate Cohnin New YorkNew ballots from Clark County (that’s Las Vega...NV...NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN1332Nate Cohnin New YorkThe latest Arizona ballot releases aren’t look...AZ...NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN2331Nick Corasanitiin PhiladelphiaThere are still 102,000 mail ballots to be cou...PA...NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN3330Dave Philippsin Las VegasBiden nets 2,520 votes in the Las Vegas area, ...NV...NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN4329Katie Glueckin Wilmington, Del.I’m told Biden spent the day watching election......NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN..................................................................4445Nate Cohnin New YorkTonight, the needle will be back — sort of. We...https://www.nytimes.com/2020/11/02/upshot/need...Learn more about the needle...NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN4454Sheera Frenkelin Silicon ValleyTimes tech reporters will be monitoring for mi......NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN4463Michael Barbaroin New YorkFrom 4 p.m. to 8 p.m. Eastern time, we’ll be t...https://nytimes.com/thedailyListen here...NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN4472Trip Gabrielin Butler County, Pa.A look at Trump and the G.O.P.’s closing strat...https://www.nytimes.com/2020/11/02/us/politics......NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN4481Shane Goldmacherin New Hope, Pa.Here’s our recap of the final day of campaigni...https://www.nytimes.com/2020/11/02/us/politics......NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN449 rows × 31 columns