In [1]:
try:
import settings
assert type(settings.CENSUS_KEY) == str or type(settings.CENSUS_KEY) == unicode
except Exception as e:
print ("error in importing settings to get at settings.CENSUS_KEY", e)
In [2]:
from census import Census
from us import states
c = Census(settings.CENSUS_KEY)
Does the census
module need to be updated to accomodate Census Bureau's API Updated with 2013 ACS (1-yr) and New Features? Maybe. I may need to formulate URL by hand.
American Community Survey 1 Year Data --> latest, greatest:
for 2012, sample call:
http://api.census.gov/data/2012/acs1/profile?get=DP02_0001PE&for=state:*&key=...
In [3]:
# 2012 ACS-1year sample call
url = "http://api.census.gov/data/2012/acs1/profile?get=DP02_0001PE&for=state:*&key={key}".format(key=settings.CENSUS_KEY)
import requests
r = requests.get(url)
r.content
Out[3]:
In [4]:
# http://api.census.gov/data/2013/acs1/profile/variables/DP02_0001PE.json exists
requests.get("http://api.census.gov/data/2013/acs1/profile/variables/DP02_0001PE.json").json()
Out[4]:
In [5]:
# 2013 ACS-1year sample call
url = "http://api.census.gov/data/2013/acs1/profile?get=DP02_0001PE&for=state:*&key={key}".format(key=settings.CENSUS_KEY)
import requests
r = requests.get(url)
r.content
Out[5]:
In [6]:
# trying to get total population for 2013-1year
# http://censusreporter.org/data/table/?table=B01003&geo_ids=04000US06&primary_geo_id=04000US06
# reports 38,332,521 +/- 0
requests.get("http://api.census.gov/data/2013/acs1/variables/B01003_001E.json").json()
Out[6]:
In [7]:
url = "http://api.census.gov/data/2013/acs1/profile?get=B01003_001E&for=state:*&key={key}".format(key=settings.CENSUS_KEY)
r = requests.get(url)
r.content
Out[7]:
In [8]:
# how about from the following tutorial:
# http://www.digital-geography.com/many-people-live-area/#.VDrwINTF8ds
Census(settings.CENSUS_KEY, year=2012).acs.get(["B01003_001E"],{'for':'state:*'})
Out[8]:
In [9]:
import json
fp = open("/Users/raymondyee/Downloads/variables_acs1_2013.json")
variables_acs1_2013 = json.load(fp)
In [ ]:
len(variables_acs1_2013['variables'].keys())
In [ ]:
'B01003_001E' in variables_acs1_2013['variables'].keys()
In [ ]:
variables_acs1_2013 = json.load
In [ ]:
# trying to get the 1-year total population for 2012 --> en route to the 2013 1-year
c.acs1dp.get('DP05_0004E', {'for': 'state:*'})
In [ ]:
c.acs.get('B01001_004E', {'for': 'state:*'})
In [ ]:
# age and sex
# http://censusreporter.org/data/table/?table=B01001&geo_ids=16000US0606000&primary_geo_id=16000US0606000
In [ ]:
requests.get("http://api.census.gov/data/2013/acs1/variables/B01001_001E.json").json()
In [ ]:
url = "http://api.census.gov/data/2013/acs1/profile?get=B01001_001E&for=state:*&key={key}".format(key=settings.CENSUS_KEY)
r = requests.get(url)
r.content
I was looking at the wrong place!!
Summary File: API Call: http://api.census.gov/data/2013/acs1? 2013 ACS Summary File Variables [ html | xml | json ] ACS Technical Documentation
Data Profile: Example Call: http://api.census.gov/data/2013/acs1/profile?get=DP02_0001PE&for=state:*&key=... 2013 ACS Data Profile Variables [ html | xml | json ] ACS Technical Documentation
In [ ]:
requests.get("http://api.census.gov/data/2013/acs1/profile/variables/DP05_0028E.json").json()
In [ ]:
url = "http://api.census.gov/data/2013/acs1/profile?get=DP05_0028E&for=state:*&key={key}".format(key=settings.CENSUS_KEY)
r = requests.get(url)
r.json()
In [ ]:
c.acs1dp.get('DP05_0028E', {'for': 'state:*'}, year=2013)
In [ ]:
# how to get the total population of Berkeley, CA
# let's do Alameda County first
requests.get("http://api.census.gov/data/2013/acs1/profile?get=DP05_0028E,NAME&for=county:001&in=state:06").json()
In [ ]:
requests.get("http://api.census.gov/data/2013/acs1/profile?get=DP05_0028E,NAME&for=place:06000&in=state:06").json()
In [ ]:
# age
# http://censusreporter.org/profiles/16000US0606000-berkeley-ca/#age
requests.get("http://api.census.gov/data/2013/acs1/profile/variables/DP05_0004E.json").json()
In [ ]:
r = requests.get("http://api.census.gov/data/2013/acs1/profile?get=DP05_0028E,DP05_0004E,DP05_0005E,NAME&for=place:06000&in=state:06").json()
r
In [ ]:
from itertools import izip
d = dict(izip(r[0],r[1]))
d
In [ ]:
# number of population 0->9
int(d[u'DP05_0004E']) + int(d[u'DP05_0004E'])
In [ ]: