# set up your census object # example from https://github.com/sunlightlabs/census from census import Census from us import states import settings c = Census(settings.CENSUS_KEY) for (i, state) in enumerate(states.STATES): print i, state.name, state.fips import requests # get the total population of all states url = "http://api.census.gov/data/2010/sf1?key={key}&get=P0010001,NAME&for=state:*".format(key=settings.CENSUS_KEY) r = requests.get(url) r.json()[:5] c.sf1.get(('NAME', 'P0010001'), {'for': 'state:%s' % states.CA.fips}) # You should understand how this works. import pandas as pd from pandas import DataFrame import census import settings import us from itertools import islice c=census.Census(settings.CENSUS_KEY) def places(variables="NAME"): for state in us.states.STATES: print state geo = {'for':'place:*', 'in':'state:{s_fips}'.format(s_fips=state.fips)} for place in c.sf1.get(variables, geo=geo): yield place r = list(islice(places("NAME,P0010001"), None)) places_df = DataFrame(r) places_df.P0010001 = places_df.P0010001.astype('int') places_df['FIPS'] = places_df.apply(lambda s: s['state']+s['place'], axis=1) print "number of places", len(places_df) print "total pop", places_df.P0010001.sum() places_df.head() assert places_df.P0010001.sum() == 228457238 # number of places in 2010 Census assert len(places_df) == 29261