Exemple #1
0
    census.api_key.key = f.readlines()[0]

# In Texas, looking at 2018 ACS block groups.
shapefile_dir = census.get_shapefile(
    geography=census.Geography.BLOCKGROUP,
    state="tx",
    year=2018,
    cache=True,
)

# Extract and reformat census data
data = census.get_acs(
    geography=census.Geography.BLOCKGROUP,
    variables=["NAME", "B03003_001E", "B03003_002E", "B03003_003E"],
    year=2018,
    dataset=census.DataSets.ACS5_DETAIL,
    state="tx",
    county="201",  # Harris County
    cache=True,
)

# The shapefile GEOID is coded with 11 digits (STATE:2 + COUNTY:3 + TRACT:6 + BLOCK_GROUP:1 = GEOID:12)
# More detail here: # https://www.census.gov/programs-surveys/geography/guidance/geo-identifiers.html
#
# To make it possible to join the census data with the shapefile, we need to make
# a column that matches between the census data and the shapefile properties. For this
# example, we can simply combine the census data state, county, and tract
data["GEOID"] = [
    s + c + t + bg
    for s, c, t, bg in zip(
        data["state"], data["county"], data["tract"], data["block group"]
Exemple #2
0
# A good place to start looking for that data is on the census data set website:
# https://www.census.gov/data/developers/data-sets.html
#
# I selected American Community Survey 5-Year data, and took a look at the "Data Profiles"
# that were available. We can see that income is coded as "DP03_0062E":
# https://api.census.gov/data/2018/acs/acs5/profile/variables.html
#
# For reference, you can use the following census API call to get that data.
# https://api.census.gov/data/2018/acs/acs5/profile?get=NAME,DP03_0062E&for=tract:*&in=state:08&in=county:*
#
# We can retreive that data with the following call
data = census.get_acs(
    geography=census.Geography.TRACT,
    variables=["NAME", "DP03_0062E"],
    year=2018,
    dataset=census.DataSets.ACS5_PROFILE,
    state="co",
    county="069",  # Larimer County
    cache=True,
)

# Since we want to show data in colorado, per census tract, we can get that from this site:
# https://www.census.gov/cgi-bin/geo/shapefiles/index.php
#
# We can also retreive it automatically with the following call
shapefile_path = census.get_shapefile(
    geography=census.Geography.TRACT,
    state="co",
    year=2019,
    cache=True,
)
    "B02001_002E": "Race, white",
    "B02001_003E": "Race, black or african american",
    "B02001_004E": "Race, american indian or alaska native",
    "B02001_005E": "Race, asian",
    "B02001_006E": "Race, native hawaiian or pacific islander",
    "B02001_007E": "Race, other",
    "B02001_008E": "Race, two or more",
    "B02001_009E": "Race, two or more, excluding 'other'",
}

# Extract and reformat census data
data = census.get_acs(
    geography=census.Geography.ZCTA,
    variables=list(variables.keys()),
    year=2018,
    dataset=census.DataSets.ACS5_DETAIL,
    # state="tx",
    # county="201": "Harris County
    cache=True,
)

# Get the shapefile
shapefile_dir = census.get_shapefile(
    geography=census.Geography.ZCTA,
    state="tx",
    year=2018,
    cache=True,
)

# We only want specific zctas
# In the future the bbd library should be able to handle this internally
Exemple #4
0
from pathlib import Path
from pprint import pprint

from bbd import census

# For convenience and security so people don't go accidentally commiting their census
# api keys, just store it as the first line of a file named # `census_api_key.txt`
# in the same directory as this file and it will be read in automatically.
api_key_file = Path(__file__).parent.absolute() / "census_api_key.txt"
with open(api_key_file, "r") as f:
    census.api_key.key = f.readlines()[0]

data = census.get_acs(
    geography=census.Geography.STATE,
    variables="NAME,B03003_001E",
    year=2018,
    dataset=census.DataSets.ACS5_DETAIL,
)

pprint(data)