06 Using specialized packages to grab data

Previously we saw how 3rd party packages vastly increase what Python can do quickly and fairly easily. Here we see that this applies to downloading data as well; someone has written a Python package to download Census data quite easily.

First we need to install the packages, and then we'll use them to grab some data. One note, however, is that to use these packages you need to sign up for a [free] Census API key. You can do this here: https://api.census.gov/data/key_signup.html

Documentation for these packages is here: https://pypi.python.org/pypi/census https://github.com/datamade/census

We'll discuss APIs, such as ths Census API next...


In [ ]:
#Import the 'census' package; install if needed
try:
    from census import Census
except:
    import pip
    pip.main(['install','census'])
    from census import Census

In [ ]:
#And we'll preview 'ggplot', import/install it...
try:
    import ggplot as gg
except:
    import pip
    pip.main(['install','ggplot'])
    import ggplot as gg

In [ ]:
#And finally, import pandas
import pandas as pd

In [ ]:
# Add your census key here:
key = None

In [ ]:
# Create the connection to the Census API
c = Census(key, year=2015)

In [ ]:
variables = ('NAME', 'B19001_001E')
params = {'for':'tract:*', 'in':'state:24'}
response = c.acs5.get(variables, params)
response = pd.DataFrame(response)
response.dtypes

In [ ]:
import ggplot as gg

response[variables[1]] = pd.to_numeric(response[variables[1]])
gg.ggplot(response, gg.aes(x = 'county', y = variables[1])) + gg.geom_boxplot()

In [ ]: