#!/usr/bin/env python # coding: utf-8 # https://api.census.gov/data/2010/dec/sf1/examples.html # # Example of extracting census attribute data (Block population and block black population), e.g. mimicking AFF query and extraction. # In[1]: #Imports import pandas as pd import os,requests # In[2]: #Get key pdKey = pd.read_csv('{}/APIkeys.csv'.format(os.environ['localappdata'])) census = pdKey.iloc[0,1] # List of variables: # P003001: Total # P003003: Black or African American alone # In[3]: #Census API theURL = 'https://api.census.gov/data/2010/dec/sf1' params = {'get':'P003001,P003003,P010001,P010004', 'for':'block:*', 'in':'state:37%county:183', 'key':census } r = requests.get(theURL,params) # In[9]: #Convert response to a dataframe rJSON =r.json() dfB = pd.DataFrame(rJSON[1:],columns=rJSON[0]) dfB['GEOID10'] = dfB.state+dfB.county+dfB.tract+dfB.block dfB.head() # In[11]: dfB[dfB.GEOID10 == '371830501001020'] # In[84]: #Export to a table dfB.iloc[:,[0,1,-1]].to_csv('Durham.csv',index=False) # Explore variables: # * Variables: https://api.census.gov/data/2010/dec/sf1/variables.html # * Groups: https://api.census.gov/data/2010/dec/sf1/groups.html # In[4]: #Get list of variables and convert to a dataframe (transposed) varsJson =requests.get('https://api.census.gov/data/2010/dec/sf1/variables.json').json() dfVars = pd.DataFrame(varsJson['variables']).T dfVars.columns # In[13]: #Reveal varibles containing the word "Black" dfVars[dfVars.label.str.contains('Black')] # In[43]: df = pd.DataFrame(varsJson['variables']).T df.head(10) # In[86]: df.loc['P010001']#:'P005012'] # In[75]: df[df.group == 'P10']