这个项目有点神秘。。。
elections的Python项目详细描述
轻松获取(美国2020年,目前)选举统计数据。在
如何使用
importpandasaspdfromelectionsimportPresident2020TimeSeries,Races2020,Election2020RawJson
总统的种族统计
^{pr2}$51
s
是一个类似字典的总统竞选界面。在
关键是国家
print(*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 wyoming
它的值是包含统计信息的数据帧。在
state='georgia'df=s[state]df
<;样式范围>;
.dataframe tbody tr th:仅类型{
垂直对齐:中间;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
<;/style>;
votes | eevp | eevp_source | trumpd | bidenj | |
---|---|---|---|---|---|
timestamp | |||||
2020-11-04T09:23:03Z | 0 | 0 | edison | 0.000 | 0.000 |
2020-11-04T00:14:11Z | 408 | 0 | edison | 0.674 | 0.326 |
2020-11-04T00:15:51Z | 127106 | 2 | edison | 0.370 | 0.618 |
2020-11-04T00:19:55Z | 173638 | 3 | edison | 0.431 | 0.557 |
2020-11-04T00:21:57Z | 174006 | 3 | edison | 0.432 | 0.557 |
... | ... | ... | ... | ... | ... |
2020-11-06T23:14:40Z | 4969860 | 99 | edison | 0.493 | 0.494 |
2020-11-06T23:17:43Z | 4969869 | 99 | edison | 0.493 | 0.494 |
2020-11-06T23:36:39Z | 4969873 | 99 | edison | 0.493 | 0.494 |
2020-11-06T23:41:44Z | 4969880 | 99 | edison | 0.493 | 0.494 |
2020-11-06T23:45:40Z | 4970093 | 99 | edison | 0.493 | 0.494 |
456行×5列
df['bidenj'].plot(figsize=(16,6),grid=True,title=state);
其他种族
但这不是这里唯一的比赛。在
s=Races2020()len(s)
51
data=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']
所以我们现在明白了,我们不仅仅是为了赢得总统竞选,而是为了。。。242场比赛(其中之一是总统竞选)。在
你还需要知道的是,President2020TimeSeries
只是给了你一个可供比赛使用的数据(“timeseries”数据),为了方便你提取和格式化,因为它可能是你来这里的主要信息。在
但是还有其他相关的(原始)数据,你可能感兴趣,也可能不感兴趣。你得到的是:
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 4556124
选举2020Rawjson
但是如果你想要更多,甚至更原始,我们可以给你。在
使用Election2020RawJson
可以访问原始的完整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'}
^{pr21}$
dict_keys(['races', 'party_control', 'liveUpdates'])
pd.DataFrame(json_data['data']['party_control']).set_index('race_type').T
<;样式范围>;
.dataframe tbody tr th:仅类型{
垂直对齐:中间;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
<;/style>;
^{tb2}$
pd.DataFrame(json_data['data']['liveUpdates'])
<;样式范围>;
.dataframe tbody tr th:仅类型{
垂直对齐:中间;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
<;/style>;
id | author | author_title_or_location | text | link_url | link_text | linked_state_1 | linked_state_2 | linked_state_3 | image_url | ... | call_type | race_id | winner | party_id | candidate_last_name | candidate_name_display | candidate_id | race_call_party_winner | state_name | link | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 333 | Nate Cohn | in New York | New ballots from Clark County (that’s Las Vega... | NV | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | |||||
1 | 332 | Nate Cohn | in New York | The latest Arizona ballot releases aren’t look... | AZ | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | |||||
2 | 331 | Nick Corasaniti | in Philadelphia | There are still 102,000 mail ballots to be cou... | PA | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | |||||
3 | 330 | Dave Philipps | in Las Vegas | Biden nets 2,520 votes in the Las Vegas area, ... | NV | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | |||||
4 | 329 | Katie Glueck | in Wilmington, Del. | I’m told Biden spent the day watching election... | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | ||||||
... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
444 | 5 | Nate Cohn | in New York | Tonight, the needle will be back — sort of. We... | https://www.nytimes.com/2020/11/02/upshot/need... | Learn more about the needle | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | ||||
445 | 4 | Sheera Frenkel | in Silicon Valley | Times tech reporters will be monitoring for mi... | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | ||||||
446 | 3 | Michael Barbaro | in New York | From 4 p.m. to 8 p.m. Eastern time, we’ll be t... | https://nytimes.com/thedaily | Listen here | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | ||||
447 | 2 | Trip Gabriel | in Butler County, Pa. | A look at Trump and the G.O.P.’s closing strat... | https://www.nytimes.com/2020/11/02/us/politics... | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | |||||
448 | 1 | Shane Goldmacher | in New Hope, Pa. | Here’s our recap of the final day of campaigni... | https://www.nytimes.com/2020/11/02/us/politics... | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
449行×31列
- 项目
标签: