Skip to main content

A toolset for political campaign data analysis

Project description

BBD: Bluebonnet Data Library

BBD is a library for helping teams perform political campaign data analysis.

Build PyPI version

Installing BBD

BBD is available on PyPI:

python -m pip install bbd

Example

For a running start, you can copy the examples folder to your machine and run the example python scripts to map median household income per census tract in Larimer county, Colorado (among other things).

Walkthrough

Follow along with this walkthrough to learn more detailed infomation about using this library.

Getting Census Data

First, find the data you want to visualize. This is often easiest through the census API, and the next bit of text will read a bit like a "How to Get Census Data" tutorial. There are plans to automate this process through the bbd library in the future.

For our working example, we'll use median household income (which is coded in the census data as as "DP03_0062E").

We can simply downloaded the json data from here and save it to your machine.

>>> census_json_path = "path/to/DP03_08_cd.json"
https://api.census.gov/data/2018/acs/acs1/profile?get=group(DP03),NAME&for=congressional%20district:*&in=state:08

For more information on how to come up with your own census API requests I'd highly recommend the first 6 minutes of this video. Or, you could simply start browsing census data sets. For reference, the following table describes the basic elements of the API call used to get this working example's data.

URL Part Meaning
get=group(DP03) Data columns include those in group DP03 (economics)
,NAME Include name of each entity, just a nicety
for=congr...district:* One row for each congressional district
in=state:08 Only get rows for state 08 (Colorado)

This census data is stored more or less as a big table in json format:

[
    ["NAME", "GEO_ID", "DP03_0001E", ...] # Headers
    ["Congressional District 1", "5001600US0801", "693303", ...] # Data
    ["Congressional District 2", "5001600US0802", "672976", ...] # Data
    ["...", "...", "...", ...] # Data
]

We'll need to transform it into a format that is plottable. To do so, the census.extract_from_json method should do the trick!

>>> from bbd import census
>>> data = census.extract_from_json(data_file, headers=["NAME", "GEO_ID", "DP03_0062E"])
>>> 
>>> print(data)
{
    "NAME": ["Congressional District 1", "Congressional District 2", ...],
    "GEO_ID": ["5001600US0801", "5001600US0802", ...]
    "DP03_0001E": ["693303", "672976", ...]
}

Getting Shapefiles

Luckily, the census provides shapefiles for pretty much every GEO_ID you can find in the census API.

Just head to this website and select the relevent geography/location.

E.g. since we are looking for Congressional Districts, you can simply select that in the dropdown and download the corresponding shapefile and save it to your machine.

>>> # Note: I went ahead and removed all but the Colorado (state 08) shapes, but that's optional
>>> shapefile_path = "path/to/tl_2019_08_cd116"

Match Census Data GEO ID to Shapefile GEO ID

I happen to know that the GEO_ID in the shapefile is stored under the key "GEOID", and is only 4 digits long. There are reasons for this described here, but all we need to do is make an entry for our census data that matches the shapefile.

>>> data["GEOID"] = [geoid[-4:] for geoid in data["GEO_ID"]]

To determine how many digits you need for your own shapefile, I'd recommend just taking a quick look with PyShp.

python -m pip install pyshp
>>> import shapefile
>>> 
>>> # What is the name of the GEO ID field?
>>> shp = shapefile.Reader(shapefile_path)
>>> shp.fields
[
    ["GEOID", "C", 4, 0], # Cool! Name of GEO ID field is "GEOID"
    ["ALAND", "N", 14, 0],
    ["AWATER", "N", 14, 0], 
    [...],
]
>>>
>>> # Now we need to see how many digits long the GEO ID is.
>>> for record in shp.records():
>>>     print(record["GEOID"])
0801
0803
0805
...
>>> # Looks like 4 digits it is!

You can also of course simply view the property table in your favorite GIS software (e.g. the free QGIS).

Create Data to Color By

The only thing better than a map is a map with color. You know the saying. The column to color by must be numeric.

>>> # Note: the name of this entry will be used as the text caption on the colormap
>>> data["Median Household Income"] = [float(x) for x in data["DP03_0062E"]]

Create Nicely Formatted String for ToolTip

We also want our map to have nice interactive tooltips so that you can see information about each shape by hovering over it with your mouse! To visualize the income, I'll just add comma separators and a $ sign.

>>> data["Median HouseHold Income (pretty format)"] = [
>>>     "${:,.2f}".format(x) for x in data["Median Household Income"]
>>> ]

Create Map!

Finally, we can create the leaflet map and save it to our machine.

>>> geojson_map = gis.make_map(
>>>     shapefile_path,
>>>     data,
>>>     join_on="GEOID", # Common field between shapefile and our census data 
>>>     color_by="Median Household Income", # Color shapes based on this data field
>>>     save_to="path/to/save/map.html", # Save leaflet map here!
>>> )

Simply open path/to/save/map.html in your favorite browser to view your map!

CO Map


Developed by Bluebonnet Data

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

bbd-0.0.4.tar.gz (14.8 kB view details)

Uploaded Source

Built Distribution

bbd-0.0.4-py3-none-any.whl (14.5 kB view details)

Uploaded Python 3

File details

Details for the file bbd-0.0.4.tar.gz.

File metadata

  • Download URL: bbd-0.0.4.tar.gz
  • Upload date:
  • Size: 14.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.49.0 CPython/3.8.5

File hashes

Hashes for bbd-0.0.4.tar.gz
Algorithm Hash digest
SHA256 8e8123a6597b215925e94d57b7b133542d1d75b4823194ca1e6435044bfc22a5
MD5 f73f884adb0419354abeaf0e4f4fe359
BLAKE2b-256 85d821f81f9c236c0d81184d53282807259436ed46ef2335aa7281256519c45f

See more details on using hashes here.

File details

Details for the file bbd-0.0.4-py3-none-any.whl.

File metadata

  • Download URL: bbd-0.0.4-py3-none-any.whl
  • Upload date:
  • Size: 14.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.49.0 CPython/3.8.5

File hashes

Hashes for bbd-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 1582d3ec04a39733136a04ebce9616f9d912778ac85bc3ec26905742a4b76e15
MD5 65760c822877acf4f61d67f0ca48c236
BLAKE2b-256 5348758a825945badd37636473edc49f3d0c89bc3731e4af79ef4cb74b6b35f7

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page