This tutorial will teach you how to join POI datasets with Placekey using Python in a Google Colab environment.
Before moving forward, make sure you are familiar with Placekey. There are a growing number of resources available:
Imports and Installations
In the first code block, we install the placekey package in our Google Colab environment and import the necessary packages.
Run this code block to authenticate yourself with Google, giving you access to the datasets.
Set API key
Replace the asterisks below with your Placekey API key. If you don’t have one yet, it’s completely free.
This tutorial uses two POI datasets: SafeGraph Patterns and Forsyth County polling locations/results.
First, define a couple functions to make it easier to read in the datasets.
The Forsyth polling dataset comes from the Forsyth County website and the Georgia results website. It includes some address columns and three columns indicating in-person vote totals for each presidential candidate on Election Day. We remove one row that is for absentee ballots because it wasn’t clear from the data source whether or not these absentee ballots were delivered on Election Day.
The Patterns dataset from SafeGraph contains POI-specific foot traffic data. Along with address columns, it includes visitor counts, visitor home Census Block Groups, median distance traveled, and more. Column descriptions can be found in the documentation. The image below shows a subset of columns.
Adding Placekey to polling dataset
As can be seen above, SafeGraph Patterns comes with a Placekey column built-in. The polling dataset, on the other hand, does not have a Placekey column, so we need to add Placekeys to the polling dataset. There are several ways to add Placekeys to a dataset (including some no-code integrations!), which you can find on the Placekey website here. In this example, we will use Python’s placekey package.
Map columns to appropriate fields
In this step, we create a new dataframe with just the address columns from the polling dataset. The columns are renamed to conform to the Placekey API. More specifically, index is mapped to query_id, Location is mapped to location_name, Address is mapped to street_address, City is mapped to city, region is kept as region, and Zip is mapped to postal_code.
Convert the dataframe to JSON
Each row will be represented by a JSON object, so that it conforms to the Placekey API.
Request Placekeys from the Placekey API
After getting the responses, we convert them to a dataframe stored in df_placekeys.
Placekey has excellent, continuously improving coverage of POIs. Most datasets will have high coverage. However, you may occasionally come across POIs that are not matched with a Placekey.
In this example, four polling locations were not given a Placekey. In the above dataframe, the error column gives some troubleshooting information. Namely, the locations had invalid addresses.
This type of error mainly occurs for one of two reasons: (1) errors in the address columns or (2) the POI is not registered with Placekey.
For the purposes of this tutorial, you can ignore the missing Placekeys.
Address vs. POI
You'll notice some of your Placekeys have 15 characters, while others have just 12. A Placekey with just 12 characters corresponds to an address, while a Placekey with 15 characters corresponds to a POI at an address.
Another characteristic of Placekey is that each POI has its own unique Placekey, even if it is permanently closed. For example, a McDonald's location that used to be a Taco Bell are two different POIs with two different Placekeys, even though they existed at the same address.
Our two datasets, the Patterns dataset and polling dataset both correspond to Election Week. So we can join on just the address part of the Placekey. As a workaround, we will remove the POI portion of any Placekeys that have it. For example, 123-456@abc-def-ghi will become 123@abc-def-ghi. We do this because the Placekey API can only match some of our POIs with 12 characters (address Placekeys) instead of the full 15 characters (POI Placekeys). When we join two datasets on Placekey, we need to make sure the Placekeys are apples-to-apples (which in our example means all Placekeys are reduced to 12-character address Placekeys). This is done in the next code block.
When would we need the full POI Placekey? Some datasets might be specific to the POI over time, but not specific to the address. To go with the same example as before, suppose a McDonald’s (owned by Tom) in San Francisco used to be a Taco Bell (owned by Amy) with the same address. Suppose we also have a dataset containing data on the owners of every business in the history of San Francisco. In that case, we would need the POI part of the Placekey to distinguish between Tom’s McDonald’s and Amy’s Taco Bell. More information can be found here.
Add Placekeys back to the original polling dataset
Join locations and df_placekeys on index and query_id, respectively, while converting any POI Placekeys to just address Placekeys.
Joining on Placekey
Now, we are ready to join the POI datasets on Placekey. Before joining, we will also need to reduce all POI Placekeys in the Patterns dataset to just address Placekeys.
Now we have one dataframe connecting voting precinct results to SafeGraph Patterns visit data. This gives us powerful insight into Election Day voter preferences at the CBG level, which is enabled by joining these two POI datasets with Placekey.
While the datasets in this example are relatively small in comparison to other datasets we could have used (20 polling locations and 2462 POIs in SafeGraph Patterns), joining them without Placekey would be very tough. Short of manually going through each row, the de-facto standard is to use a third-party address matching service that can vary on address quality and cost. Alternatively, Placekey solves this same problem (linking data) by providing a free and open universal standard identifier. So, we can see how the value of Placekey scales exponentially: the faster it is adopted the more data we will have for problem-solving. That is all to say, whether you have 20 POIs or 200,000 POIs, Placekey enables you to effectively join POI datasets (for free).