In [35]:
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 [36]:
states_fips = np.array([state.fips for state in us.states.STATES])

In [42]:
## EXERCISE
## FILL in with your generator for all census places in the 2010 census 
places=c.sf1.get('NAME,P0010001', 
                         geo={'for': 'place:*', 
                              'in': 'state:*'})

def places(variables="NAME",process="by_state"):
    if process == 'by_state':
    for state in states_fips:
        places=c.sf1.get('NAME,P0010001', 
                         geo={'for': 'place:*', 
                              'in': 'state:%s' % states_fips})
        for k in places:
            yield k

    else:
        for k in places:
            yield k

In [49]:
places().next()


Out[49]:
{u'NAME': u'Abanda CDP',
 u'P0010001': u'192',
 u'place': u'00100',
 u'state': u'01'}

In [47]:
# 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()


number of places 1505214
total pop 11758209936
Out[47]:
NAME P0010001 place state FIPS
0 Abanda CDP 192 00100 01 0100100
1 Abbeville city 2688 00124 01 0100124
2 Adamsville city 4522 00460 01 0100460
3 Addison town 758 00484 01 0100484
4 Akron town 356 00676 01 0100676

5 rows × 5 columns


In [48]:
# 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


---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-48-68db89ba4e02> in <module>()
      1 # if you've done this correctly, the following asserts should stop complaining
      2 
----> 3 assert places_df.P0010001.sum() == 228457238
      4 # number of places in 2010 Census
      5 assert len(places_df) == 29261

AssertionError: 

In [ ]: