<jupyter_start><jupyter_text>Downloading US Census Data from API**This notebook shows how to donwload all *blockgroup* data for a set of variable****You can change to download different geographies***It uses three external libraries:1. [Sunlight Labs Census Wrapper - Joe's Fork](https://github.com/joehand/census/) *Note: blockgroups will not  work with `pip install census` but may with other geographies.*2. [US States Library](https://pypi.python.org/pypi/us)3. Pandas<jupyter_code># may need to point this to your census module
from us_census_api.core import Census 
# Download from pypi
from us import states

from pandas import DataFrame
import pandas as pd
census_API_key = 'API_KEY_HERE'

c = Census(census_API_key, year=2010)
# Test basic API call
data = DataFrame(c.acs5.state_county_blockgroup(('NAME', 'GEOID'), 36, '5', Census.ALL))
data.head()
# variable list:

var_edu =  {
    'GEOID':'GeoId',
    'B15002_001E': 'Edu Total',
    'B15002_002E': 'Edu Male Total',
}

for i in range(3,19):
    key = 'B15002_{}E'.format(str(i).zfill(3))
    var_edu[key] = 'Edu Male Level {}'.format(i-3)

for i in range(20,36):
    key = 'B15002_{}E'.format(str(i).zfill(3))
    var_edu[key] = 'Edu Female Level {}'.format(i-20)

var_edu_tup = tuple(key for key, val in var_edu.items())
var_edu
# Test API call with variables
data = DataFrame(c.acs5.state_county_blockgroup(var_edu_tup, 36, '5', Census.ALL))
data.head()
bkgps = {}
# THIS DOWNLOADS ALL THE DATA! 
# It can take awhile. 

# If it breaks:
# 1. check what state was the last state you got. assume you only got part of that state
# 2. delete that state in bkgps dict
# 3. change states.STATES to states.STATES[5:] (or wherever it broke)
# 4. repeat

for state in states.STATES:
    print(state)
    
    counties = c.acs5.state_county('NAME', state.fips, Census.ALL)
    data = []
    for county in counties:
        county_num = county['county']
        data.extend(c.acs5.state_county_blockgroup(var_edu_tup, state.fips, county_num, Census.ALL))
    bkgps[state] = data
bkgpsDF = []
for key, val in bkgps.items():
    bkgpsDF.extend(val)
bkgpDF = DataFrame(bkgpsDF)
bkgpDF.head()
bkgpDF.to_csv('/Users/joe/Dropbox/SFI_CensusData/UnitedStates/2010acs_edu.csv', index=False)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12709620476610767, 'fraction_numerical': 0.02824360105913504, 'mean_word_length': 4.449519230769231, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '151804', 'license': 'MIT', 'n_tokens_mistral': 874, 'n_tokens_neox': 820, 'n_words': 232, 'path': 'notebooks/2010 Census API Gathering-Edu.ipynb', 'repo_name': 'joehand/statistics-neighborhoods', 'size': '2266'}