Lab 12 solutions

Lab 12 solutions#

Scraping#

import pandas as pd

tables = pd.read_html(
    "https://en.wikipedia.org/wiki/List_of_countries_and_dependencies_by_area",
    match="Country / dependency",
)
countries = tables[0]
countries
Unnamed: 0 Country / dependency Total in km2 (mi2) Land in km2 (mi2) Water in km2 (mi2) % water Unnamed: 6
0 Earth 510,072,000 (196,940,000) 148,940,000 (57,506,000) 361,132,000 (139,434,000) 70.8 NaN
1 1 Russia 17,098,246 (6,601,667) 16,376,870 (6,323,142) 721,380 (278,530) 4.2 [b]
2 Antarctica 14,200,000 (5,480,000) 14,200,000 (5,480,000) 0 0.0 [c]
3 2 Canada 9,984,670 (3,855,100) 9,093,507 (3,511,021) 891,163 (344,080) 8.9 [d]
4 3/4 [e] China 9,596,960 (3,705,410) 9,326,410 (3,600,950) 270,550 (104,460) 2.8 [f]
... ... ... ... ... ... ... ...
259 Ashmore and Cartier Islands (Australia) 5.0 (1.9) 5.0 (1.9) 0 0.0 [q]
260 Coral Sea Islands (Australia) 3.0 (1.2) 3.0 (1.2) 0 0.0 [db]
261 Spratly Islands (disputed) 2.0 (0.77) 2.0 (0.77) 0 0.0 [54]
262 194 Monaco 2.0 (0.77) 2.0 (0.77) 0 0.0 [dc]
263 195 Vatican City 0.49 (0.19) 0.49 (0.19) 0 0.0 [dd]

264 rows × 7 columns

FEC data#

Querying#

import requests

candidates_with_funds = {
    "api_key": "DEMO_KEY",
    "has_raised_funds": "true",
}
response = requests.get("https://api.open.fec.gov/v1/candidates/", params=candidates_with_funds)
data = response.json()
data
{'api_version': '1.0',
 'pagination': {'count': 26215,
  'is_count_exact': True,
  'page': 1,
  'pages': 1311,
  'per_page': 20},
 'results': [{'active_through': 2022,
   'candidate_id': 'H2CO07170',
   'candidate_inactive': False,
   'candidate_status': 'P',
   'cycles': [2022, 2024],
   'district': '07',
   'district_number': 7,
   'election_districts': ['07'],
   'election_years': [2022],
   'federal_funds_flag': False,
   'first_file_date': '2021-12-27',
   'has_raised_funds': True,
   'inactive_election_years': None,
   'incumbent_challenge': 'O',
   'incumbent_challenge_full': 'Open seat',
   'last_f2_date': '2022-08-10',
   'last_file_date': '2022-08-10',
   'load_date': '2023-03-09T10:16:03',
   'name': 'AADLAND, ERIK',
   'office': 'H',
   'office_full': 'House',
   'party': 'REP',
   'party_full': 'REPUBLICAN PARTY',
   'state': 'CO'},
  {'active_through': 2022,
   'candidate_id': 'H2UT03280',
   'candidate_inactive': False,
   'candidate_status': 'C',
   'cycles': [2022],
   'district': '03',
   'district_number': 3,
   'election_districts': ['03'],
   'election_years': [2022],
   'federal_funds_flag': False,
   'first_file_date': '2020-03-24',
   'has_raised_funds': True,
   'inactive_election_years': None,
   'incumbent_challenge': 'C',
   'incumbent_challenge_full': 'Challenger',
   'last_f2_date': '2022-03-21',
   'last_file_date': '2022-03-21',
   'load_date': '2022-04-13T21:10:09',
   'name': 'AALDERS, TIM',
   'office': 'H',
   'office_full': 'House',
   'party': 'REP',
   'party_full': 'REPUBLICAN PARTY',
   'state': 'UT'},
  {'active_through': 2018,
   'candidate_id': 'S2UT00229',
   'candidate_inactive': False,
   'candidate_status': 'P',
   'cycles': [2012, 2014, 2016, 2018, 2020],
   'district': '00',
   'district_number': 0,
   'election_districts': ['00', '00'],
   'election_years': [2012, 2018],
   'federal_funds_flag': False,
   'first_file_date': '2012-02-08',
   'has_raised_funds': True,
   'inactive_election_years': None,
   'incumbent_challenge': 'O',
   'incumbent_challenge_full': 'Open seat',
   'last_f2_date': '2018-04-23',
   'last_file_date': '2018-04-23',
   'load_date': '2019-03-27T16:02:41',
   'name': 'AALDERS, TIMOTHY NOEL',
   'office': 'S',
   'office_full': 'Senate',
   'party': 'CON',
   'party_full': 'CONSTITUTION PARTY',
   'state': 'UT'},
  {'active_through': 2020,
   'candidate_id': 'H0TX22260',
   'candidate_inactive': False,
   'candidate_status': 'C',
   'cycles': [2020],
   'district': '22',
   'district_number': 22,
   'election_districts': ['22'],
   'election_years': [2020],
   'federal_funds_flag': False,
   'first_file_date': '2019-10-17',
   'has_raised_funds': True,
   'inactive_election_years': None,
   'incumbent_challenge': 'O',
   'incumbent_challenge_full': 'Open seat',
   'last_f2_date': '2019-10-17',
   'last_file_date': '2019-10-17',
   'load_date': '2020-03-18T21:13:37',
   'name': 'AALOORI, BANGAR REDDY',
   'office': 'H',
   'office_full': 'House',
   'party': 'REP',
   'party_full': 'REPUBLICAN PARTY',
   'state': 'TX'},
  {'active_through': 1978,
   'candidate_id': 'H6PA16106',
   'candidate_inactive': False,
   'candidate_status': 'P',
   'cycles': [1976, 1978, 1980],
   'district': '16',
   'district_number': 16,
   'election_districts': ['16', '16'],
   'election_years': [1976, 1978],
   'federal_funds_flag': False,
   'first_file_date': '1976-04-12',
   'has_raised_funds': True,
   'inactive_election_years': None,
   'incumbent_challenge': None,
   'incumbent_challenge_full': None,
   'last_f2_date': '1978-07-05',
   'last_file_date': '1978-07-05',
   'load_date': '2002-03-30T00:00:00',
   'name': 'AAMODT, NORMAN O.',
   'office': 'H',
   'office_full': 'House',
   'party': 'REP',
   'party_full': 'REPUBLICAN PARTY',
   'state': 'PA'},
  {'active_through': 2012,
   'candidate_id': 'H2CA01110',
   'candidate_inactive': False,
   'candidate_status': 'P',
   'cycles': [2012, 2014, 2016],
   'district': '01',
   'district_number': 1,
   'election_districts': ['01'],
   'election_years': [2012],
   'federal_funds_flag': False,
   'first_file_date': '2012-02-22',
   'has_raised_funds': True,
   'inactive_election_years': None,
   'incumbent_challenge': 'C',
   'incumbent_challenge_full': 'Challenger',
   'last_f2_date': '2012-02-22',
   'last_file_date': '2012-02-22',
   'load_date': '2013-04-26T09:04:30',
   'name': 'AANESTAD, SAMUEL',
   'office': 'H',
   'office_full': 'House',
   'party': 'REP',
   'party_full': 'REPUBLICAN PARTY',
   'state': 'CA'},
  {'active_through': 2018,
   'candidate_id': 'H8CO06237',
   'candidate_inactive': False,
   'candidate_status': 'C',
   'cycles': [2018],
   'district': '06',
   'district_number': 6,
   'election_districts': ['06'],
   'election_years': [2018],
   'federal_funds_flag': False,
   'first_file_date': '2017-04-26',
   'has_raised_funds': True,
   'inactive_election_years': None,
   'incumbent_challenge': 'C',
   'incumbent_challenge_full': 'Challenger',
   'last_f2_date': '2017-04-26',
   'last_file_date': '2017-04-26',
   'load_date': '2017-08-01T20:57:28',
   'name': 'AARESTAD, DAVID',
   'office': 'H',
   'office_full': 'House',
   'party': 'DEM',
   'party_full': 'DEMOCRATIC PARTY',
   'state': 'CO'},
  {'active_through': 2008,
   'candidate_id': 'P80002926',
   'candidate_inactive': False,
   'candidate_status': 'N',
   'cycles': [2006, 2008, 2010, 2012, 2014, 2016],
   'district': '00',
   'district_number': 0,
   'election_districts': ['00'],
   'election_years': [2008],
   'federal_funds_flag': False,
   'first_file_date': '2005-10-12',
   'has_raised_funds': True,
   'inactive_election_years': None,
   'incumbent_challenge': 'O',
   'incumbent_challenge_full': 'Open seat',
   'last_f2_date': '2007-03-13',
   'last_file_date': '2007-03-13',
   'load_date': '2016-11-17T06:10:48',
   'name': 'AARON, LAURA DAVIS',
   'office': 'P',
   'office_full': 'President',
   'party': 'DEM',
   'party_full': 'DEMOCRATIC PARTY',
   'state': 'US'},
  {'active_through': 2024,
   'candidate_id': 'H4OR05312',
   'candidate_inactive': False,
   'candidate_status': 'C',
   'cycles': [2024],
   'district': '05',
   'district_number': 5,
   'election_districts': ['05'],
   'election_years': [2024],
   'federal_funds_flag': False,
   'first_file_date': '2023-07-24',
   'has_raised_funds': True,
   'inactive_election_years': None,
   'incumbent_challenge': 'C',
   'incumbent_challenge_full': 'Challenger',
   'last_f2_date': '2023-07-24',
   'last_file_date': '2023-07-24',
   'load_date': '2023-10-16T21:03:02',
   'name': 'AASEN, ANDREW J',
   'office': 'H',
   'office_full': 'House',
   'party': 'NON',
   'party_full': 'NON-PARTY',
   'state': 'OR'},
  {'active_through': 2024,
   'candidate_id': 'H2CA30291',
   'candidate_inactive': False,
   'candidate_status': 'N',
   'cycles': [2022, 2024],
   'district': '32',
   'district_number': 32,
   'election_districts': ['32', '32'],
   'election_years': [2022, 2024],
   'federal_funds_flag': False,
   'first_file_date': '2021-01-16',
   'has_raised_funds': True,
   'inactive_election_years': None,
   'incumbent_challenge': 'C',
   'incumbent_challenge_full': 'Challenger',
   'last_f2_date': '2022-07-15',
   'last_file_date': '2022-07-15',
   'load_date': '2023-01-12T22:24:01',
   'name': 'AAZAMI, SHERVIN',
   'office': 'H',
   'office_full': 'House',
   'party': 'DEM',
   'party_full': 'DEMOCRATIC PARTY',
   'state': 'CA'},
  {'active_through': 2022,
   'candidate_id': 'H2MN07162',
   'candidate_inactive': False,
   'candidate_status': 'P',
   'cycles': [2022, 2024],
   'district': '07',
   'district_number': 7,
   'election_districts': ['07'],
   'election_years': [2022],
   'federal_funds_flag': False,
   'first_file_date': '2022-06-06',
   'has_raised_funds': True,
   'inactive_election_years': None,
   'incumbent_challenge': 'C',
   'incumbent_challenge_full': 'Challenger',
   'last_f2_date': '2022-06-06',
   'last_file_date': '2022-06-06',
   'load_date': '2023-03-09T10:16:03',
   'name': 'ABAHSAIN, JILL',
   'office': 'H',
   'office_full': 'House',
   'party': 'DFL',
   'party_full': 'DEMOCRATIC-FARMER-LABOR',
   'state': 'MN'},
  {'active_through': 2000,
   'candidate_id': 'H0MA01024',
   'candidate_inactive': False,
   'candidate_status': 'P',
   'cycles': [2000, 2002, 2004],
   'district': '01',
   'district_number': 1,
   'election_districts': ['01'],
   'election_years': [2000],
   'federal_funds_flag': False,
   'first_file_date': '2000-02-02',
   'has_raised_funds': True,
   'inactive_election_years': None,
   'incumbent_challenge': 'C',
   'incumbent_challenge_full': 'Challenger',
   'last_f2_date': '2000-02-02',
   'last_file_date': '2000-02-02',
   'load_date': '2002-04-12T00:00:00',
   'name': 'ABAIR, PETER JON',
   'office': 'H',
   'office_full': 'House',
   'party': 'REP',
   'party_full': 'REPUBLICAN PARTY',
   'state': 'MA'},
  {'active_through': 2008,
   'candidate_id': 'H6NJ05155',
   'candidate_inactive': False,
   'candidate_status': 'C',
   'cycles': [2006, 2008],
   'district': '05',
   'district_number': 5,
   'election_districts': ['05', '05'],
   'election_years': [2006, 2008],
   'federal_funds_flag': False,
   'first_file_date': '2006-03-28',
   'has_raised_funds': True,
   'inactive_election_years': None,
   'incumbent_challenge': 'C',
   'incumbent_challenge_full': 'Challenger',
   'last_f2_date': '2007-06-05',
   'last_file_date': '2007-06-05',
   'load_date': '2009-04-29T00:00:00',
   'name': 'ABATE, CAMILLE M',
   'office': 'H',
   'office_full': 'House',
   'party': 'DEM',
   'party_full': 'DEMOCRATIC PARTY',
   'state': 'NJ'},
  {'active_through': 1992,
   'candidate_id': 'H2NJ12036',
   'candidate_inactive': False,
   'candidate_status': 'P',
   'cycles': [1992, 1994, 1996, 1998],
   'district': '12',
   'district_number': 12,
   'election_districts': ['12'],
   'election_years': [1992],
   'federal_funds_flag': False,
   'first_file_date': '1992-04-15',
   'has_raised_funds': True,
   'inactive_election_years': None,
   'incumbent_challenge': 'C',
   'incumbent_challenge_full': 'Challenger',
   'last_f2_date': '1992-04-15',
   'last_file_date': '1992-04-15',
   'load_date': '2002-04-03T00:00:00',
   'name': 'ABATE, FRANK G',
   'office': 'H',
   'office_full': 'House',
   'party': 'DEM',
   'party_full': 'DEMOCRATIC PARTY',
   'state': 'NJ'},
  {'active_through': 1990,
   'candidate_id': 'H0IA03071',
   'candidate_inactive': False,
   'candidate_status': 'C',
   'cycles': [1990],
   'district': '03',
   'district_number': 3,
   'election_districts': ['03'],
   'election_years': [1990],
   'federal_funds_flag': False,
   'first_file_date': '1990-05-01',
   'has_raised_funds': True,
   'inactive_election_years': None,
   'incumbent_challenge': 'C',
   'incumbent_challenge_full': 'Challenger',
   'last_f2_date': '1990-05-01',
   'last_file_date': '1990-05-01',
   'load_date': '2002-03-30T00:00:00',
   'name': 'ABBAS, JEFFREY LYN',
   'office': 'H',
   'office_full': 'House',
   'party': 'REP',
   'party_full': 'REPUBLICAN PARTY',
   'state': 'IA'},
  {'active_through': 2024,
   'candidate_id': 'H4CA32152',
   'candidate_inactive': False,
   'candidate_status': 'N',
   'cycles': [2024],
   'district': '32',
   'district_number': 32,
   'election_districts': ['32'],
   'election_years': [2024],
   'federal_funds_flag': False,
   'first_file_date': '2023-12-03',
   'has_raised_funds': True,
   'inactive_election_years': None,
   'incumbent_challenge': 'C',
   'incumbent_challenge_full': 'Challenger',
   'last_f2_date': '2024-02-07',
   'last_file_date': '2024-02-07',
   'load_date': '2024-02-08T21:02:32',
   'name': 'ABBITT, DAVE',
   'office': 'H',
   'office_full': 'House',
   'party': 'DEM',
   'party_full': 'DEMOCRATIC PARTY',
   'state': 'CA'},
  {'active_through': 1994,
   'candidate_id': 'H4TX15043',
   'candidate_inactive': False,
   'candidate_status': 'C',
   'cycles': [1994],
   'district': '15',
   'district_number': 15,
   'election_districts': ['15'],
   'election_years': [1994],
   'federal_funds_flag': False,
   'first_file_date': '1994-01-14',
   'has_raised_funds': True,
   'inactive_election_years': None,
   'incumbent_challenge': 'C',
   'incumbent_challenge_full': 'Challenger',
   'last_f2_date': '1994-01-14',
   'last_file_date': '1994-01-14',
   'load_date': '2002-03-30T00:00:00',
   'name': 'ABBOTT, BONNIE',
   'office': 'H',
   'office_full': 'House',
   'party': 'REP',
   'party_full': 'REPUBLICAN PARTY',
   'state': 'TX'},
  {'active_through': 1996,
   'candidate_id': 'H6SD00077',
   'candidate_inactive': False,
   'candidate_status': 'P',
   'cycles': [1996, 1998],
   'district': '01',
   'district_number': 1,
   'election_districts': ['00'],
   'election_years': [1996],
   'federal_funds_flag': False,
   'first_file_date': '1995-10-02',
   'has_raised_funds': True,
   'inactive_election_years': None,
   'incumbent_challenge': 'C',
   'incumbent_challenge_full': 'Challenger',
   'last_f2_date': '1995-10-02',
   'last_file_date': '1995-10-02',
   'load_date': '2002-04-03T00:00:00',
   'name': 'ABBOTT, JAMES W',
   'office': 'H',
   'office_full': 'House',
   'party': 'DEM',
   'party_full': 'DEMOCRATIC PARTY',
   'state': 'SD'},
  {'active_through': 1988,
   'candidate_id': 'S6CA00345',
   'candidate_inactive': False,
   'candidate_status': 'N',
   'cycles': [1986, 1988],
   'district': '00',
   'district_number': 0,
   'election_districts': ['00', '00'],
   'election_years': [1986, 1988],
   'federal_funds_flag': False,
   'first_file_date': '1988-05-16',
   'has_raised_funds': True,
   'inactive_election_years': None,
   'incumbent_challenge': 'C',
   'incumbent_challenge_full': 'Challenger',
   'last_f2_date': '1988-08-29',
   'last_file_date': '1988-08-29',
   'load_date': '2005-05-26T00:00:00',
   'name': 'ABBOTT, JOHN HANCOCK',
   'office': 'S',
   'office_full': 'Senate',
   'party': 'DEM',
   'party_full': 'DEMOCRATIC PARTY',
   'state': 'CA'},
  {'active_through': 1992,
   'candidate_id': 'P80002579',
   'candidate_inactive': False,
   'candidate_status': 'P',
   'cycles': [1988, 1990, 1992, 1994, 1996, 1998],
   'district': '00',
   'district_number': 0,
   'election_districts': ['00', '00'],
   'election_years': [1988, 1992],
   'federal_funds_flag': True,
   'first_file_date': '1988-08-29',
   'has_raised_funds': True,
   'inactive_election_years': None,
   'incumbent_challenge': 'C',
   'incumbent_challenge_full': 'Challenger',
   'last_f2_date': '1992-06-16',
   'last_file_date': '1992-06-16',
   'load_date': '2002-04-03T00:00:00',
   'name': 'ABBOTT, JOHN HANCOCK',
   'office': 'P',
   'office_full': 'President',
   'party': 'DEM',
   'party_full': 'DEMOCRATIC PARTY',
   'state': 'US'}]}