In [1]:
import pandas as pd
from pandas import DataFrame
import census
import settings
import us
import numpy as np
from itertools import islice
# instantiate the census object
c=census.Census(settings.CENSUS_KEY)
In [2]:
states_fips = np.array([state.fips for state in us.states.STATES])
In [3]:
11 in states_fips
Out[3]:
In [4]:
all_places=c.sf1.get('NAME,P0010001',
geo={'for': 'place:*',
'in': 'state:*'})
In [5]:
## EXERCISE
## FILL in with your generator for all census places in the 2010 census
def places(variables="NAME"):
# global all_places
# if process == 'by_state':
for state in states_fips:
p=c.sf1.get('NAME,P0010001',
geo={'for': 'place:*',
'in': 'state:%s' % state})
for k in p:
yield k
# else:
# print"using all_places"
# for k in all_places:
# yield k
In [6]:
#for testing the generator
place_gen=places()
In [7]:
#for testing the generator
place_gen.next()
Out[7]:
In [8]:
# use this code to run your code
# I recommend replacing the None in islice to a small number to make sure you're on
# the right track
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()
Out[8]:
In [100]:
# if you've done this correctly, the following asserts should stop complaining
assert places_df.P0010001.sum() == 228457238
# number of places in 2010 Census
assert len(places_df) == 29261
In [ ]: