ACS Demo
Introduction
This notebook demonstrates how to load US Census American Community Survey (ACS) 5-year data and do demographic analysis on it. The process is very much parallel to how we loaded and used US Census redistricting data in the SoMa DIS Demo and Seeing White notebooks.
Imports and configuration
[1]:
# So we can run from within the censusdis project and find the packages we need.
import os
import sys
sys.path.append(
os.path.join(os.path.abspath(os.path.join(os.path.curdir, os.path.pardir)))
)
[2]:
import censusdis.data as ced
import censusdis.states
from censusdis.maps import ShapeReader, clip_to_states
import divintseg as dis
[3]:
# Set your API key here.
CENSUS_API_KEY = None
[4]:
SHAPEFILE_ROOT = os.path.join(os.environ["HOME"], "data", "shapefiles")
# Make sure it is there.
os.makedirs(SHAPEFILE_ROOT, exist_ok=True)
ced.set_shapefile_path(SHAPEFILE_ROOT)
[5]:
YEAR = 2019
DATASET = "acs/acs5"
[6]:
# Feel free to try other states.
STATE = censusdis.states.STATE_NJ
Make a metadata call to find out what fields are available
The group we chose is B03002, which has race and ethnicity estimates down to the census block group level.
[7]:
GROUP = "B03002"
[8]:
leaves = ced.variables.group_leaves(DATASET, YEAR, GROUP)
Load the data and compute diversity and integration
[9]:
df_acs5 = ced.download_detail(
DATASET,
YEAR,
leaves,
state=STATE,
block_group="*",
api_key=CENSUS_API_KEY,
)
[10]:
df_di = dis.di(
df_acs5,
by=["STATE", "COUNTY", "TRACT"],
over="BLOCK_GROUP",
).reset_index()
df_di.head()
[10]:
| STATE | COUNTY | TRACT | diversity | integration | |
|---|---|---|---|---|---|
| 0 | 34 | 001 | 000100 | 0.799079 | 0.742759 |
| 1 | 34 | 001 | 000200 | 0.774784 | 0.701973 |
| 2 | 34 | 001 | 000300 | 0.781342 | 0.726768 |
| 3 | 34 | 001 | 000400 | 0.765627 | 0.687127 |
| 4 | 34 | 001 | 000500 | 0.695903 | 0.685162 |
Add geography to our dataframe
[11]:
gdf_di = ced.add_inferred_geography(df_di, YEAR)
Plot the data for the state
[12]:
import matplotlib.pyplot as plt
plt.rcParams["figure.figsize"] = (8, 8)
ax = gdf_di.plot(
"diversity",
cmap="hot",
legend=True,
vmin=0.0,
vmax=1.0,
)
ax.set_title(f"Diversity in {censusdis.states.STATE_NAMES_FROM_IDS[STATE]}")
ax.tick_params(
left=False,
right=False,
bottom=False,
labelleft=False,
labelbottom=False,
)
[ ]: