-
Notifications
You must be signed in to change notification settings - Fork 0
/
bea_examples.py
68 lines (51 loc) · 2.07 KB
/
bea_examples.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import os, dotenv;
import requests, json;
import numpy as np;
import pandas as pd;
#
# Load API Key from .env file
#
dotenv.load_dotenv();
bea_api_key = os.getenv('bea_api_key');
bea_endpoint = 'https://apps.bea.gov/api/data';
#
# Fetch data for CAINC4 (Personal income and employment by major component)
#
# UserID=Your-36Character-Key&
# method=GetData&datasetname=Regional&
# TableName=CAINC4&LineCode=30&
# GeoFIPS=COUNTY&
# Year=2013&
# ResultFormat=json&
r = requests.get(
url=bea_endpoint,
params={
'UserID': bea_api_key,
'method': 'GetData',
'datasetname':'Regional',
'TableName':'CAINC4',
'LineCode':'30',
'GeoFIPS':'COUNTY',
'Year':'2013',
'ResultFormat':'json'
}
);
r_json = r.json()['BEAAPI'];
r_data_name = r_json['Results']['PublicTable'];
print('Data comes from table: ', r_data_name); # Data comes from table: CAINC4 Personal income and employment by major component
r_data = pd.DataFrame(r_json['Results']['Data']);
# >>> r_data
#
# Code GeoFips GeoName TimePeriod CL_UNIT UNIT_MULT DataValue NoteRef
# 0 CAINC4-30 01001 Autauga, AL 2013 Dollars 0 35492 4
# 1 CAINC4-30 01003 Baldwin, AL 2013 Dollars 0 38828 4
# 2 CAINC4-30 01005 Barbour, AL 2013 Dollars 0 29719 4
# 3 CAINC4-30 01007 Bibb, AL 2013 Dollars 0 27225 4
# 4 CAINC4-30 01009 Blount, AL 2013 Dollars 0 30222 4
# ... ... ... ... ... ... ... ... ...
# 3135 CAINC4-30 56037 Sweetwater, WY 2013 Dollars 0 48578 4
# 3136 CAINC4-30 56039 Teton, WY 2013 Dollars 0 177810 4
# 3137 CAINC4-30 56041 Uinta, WY 2013 Dollars 0 39430 4
# 3138 CAINC4-30 56043 Washakie, WY 2013 Dollars 0 42240 4
# 3139 CAINC4-30 56045 Weston, WY 2013 Dollars 0 44118 4
# [3140 rows x 8 columns]