Skip to Main Content

Census Data

This guide provides an introduction to U.S. census data: concepts, datasets, and data sources

Census Data API

The Census Bureau publishes REST APIs for many datasets. Instead of interacting with a graphic interface to filter and download tables, you can write scripts in any scripting language to create specific extracts of variables and pull them directly into a program where you can process, analyze, and save the data. The mechanics of using the API are straightforward; the challenge is understanding how the census is structured in datasets, tables, geographies, and subjects, and how individual datasets are structured so that you can build a proper request. Request a free API key to exceed the daily request minimum.

Census API Python Example

In the following minimally working example, python is used to extract 2019 population estimates data for Rhode Island counties. The 3rd party requests module is used to interact with the API. The Vintage 2019 Population Estimates API page contains lists of supported geographies and variables for building requests. Knowledge of ANSI FIPS codes is necessary for specifying geography, in this case the state (the code for RI is 44). As counties nest within states, all counties can be selected using the asterisk *. Variables are saved in individual strings with commas separating the variables. A file that contains the census API key (a string of alphanumeric characters) is stored in the same directory as the script and is read into the program. It's important not to expose your key in the script.

Census API script in Python

Variables that are used to specify fairly consistent parameters like the dataset source and name are inserted into a base url, and this base url is subsequently modified by inserting parameters like geography and variables that are more likely to change in your program. The API key is inserted at the end. The requests module submits the url and returns the response as a json object, which we save as a nested list where each sublist represents a record (a population estimate for a given county for a given year in this case). We print this to the screen, and then write the output to a CSV file.

Census API retrieved data

Visit the GeoData@SciLi API Examples GitHub repository for examples of working Python code with sample output.