# Seattle School Demographics

In my freshman year of high school, my school district released a new student assignment plan, based on geography rather than choice, as had been the previous policy. I remember thinking that it would be interesting to study how the demographics of Seattle mapped onto the demographics of individual schools, but at the time I didn’t know enough about GIS and data science to do anything about it. Well now I do, so here’s an attempt.

The first task is collecting the demographic data of Seattle at the finest level of detail possible. That means collecting block-level data from the US Census Bureau. To know which Census blocks are within the Seattle city limits, we can download the shapefiles for the Seattle 2010 Census Blocks. The GeoPandas library is an incredibly handy and powerful package for dealing with geospatial data, and I’ll be using it extensively throughout this article.

Another library I’ll be using is census, which is a convenient wrapper for the US Census API. To use the US Census API, you need to request a Census API Key.

Census data is structured hierarchically: states contain counties, which contain tracts, which contain block groups, which contain blocks. The Census API has limits that restrict the level of geographic detail you can fetch from a single query. For instance, you can’t query all blocks in a county, but you can query all tracts in a county and then query all blocks in a given tract. Because of these rules, we first need to determine all tracts in Seattle, then query block-level data for each of those tracts.

Most shapefiles have a GEOID10 field that uniquely specifies a geographic entity. The GEOID10 associated with the shapefile from the City of Seattle consists of a two digit state code, a three digit county code, a six digit tract code, and a 4 digit block code. For example, the GEOID10 “530330067001001” refers to state 53 (Washington), county 033 (King County), tract 67 (Magnolia/Queen Anne), block 1001 (SOME LOCATION). In the following code block, we extract and sort all unique tract numbers:

Now we prepare the census queries. I create dicts of census variables and their corresponding descriptions, which I got from a very long list of the 2010 Census variables. The Census records Hispanic heritage differently than other ethic backgrounds, so for simplicity I’m not including that variable.

Now we loop over all tracts, querying all census variables for all blocks:

The next step is some ugly code to coalesce the census data contained in tract_responses into a single pretty GeoDataFrame. You can read it on my GitHub, but I’m omitting it here for the sake of brevity. For fun, I’ve included some plots of population density by block for different ethnic backgrounds. These choropleths were very easy to generate using the GeoDataFrame plot() method. Blocks with zero population were set to the background color.

White Black Asian

Now that we have block-level demographic data organized nicely in a GeoDataFrame, the next step is to determine which blocks are included in a given school’s attendance area boundary. We first download and extract the attendance area boundaries. An important attribute of a shapefile is its coordinate reference system (CRS), which specifies how coordinates on a spherical surface are projected onto a flat surface. These CRS are often represented as proj4 strings, such as the following, which is a nice Seattle-centered projection:

We merge our block-level GeoDataFrame with out block-level demographic DataFrame, and convert it to the SEATTLE_PROJ CRS:

We also read-in the shapefile for the High School attendance area boundaries, convert to the SEATTLE_PROJ CRS, and remove extraneous columns:

Now we loop over all school attendance area boundary geometries and all block geometries to see if the attendance area completely contains the block. To do this we employ the .contains() method for GeoPandas geometries. We store the data in a 2D boolean array of shape (number of schools, number of blocks), so that the $$i,j$$-th element is true if school boundary $$i$$ contains block $$j$$, and False otherwise:

We can plot the blocks, colored by containing school, along with the assignment area shapefiles, to make sure we performed the previous steps correctly:

Elementary School Middle School High School

The last step is to calculate the average population for the contained blocks for each school, for each race.

Finally we can plot the demographic data by school, as shown below:

Elementary School Middle School High School

My name is Tristan Lee, I'm a data science with a background in computational physics. This is my blog that I use to share simulations, visualizations, and analyses. My focus areas include right-wing extremism, social network analysis, and high-performance computing.