Tuning-In To NYC’s Musical Neighborhoods
By: Michael Cascio
Tags: Data Visualization, Data Analysis, Machine Learning, Data Science, Python

Methodology

Data Sources

NYU Spatial Data Repository: I am using the ‘2014 New York City Neighborhood Names’ dataset hosted by NYU’s Spatial Data Repository as the basis for the neighborhood names and associated location centroids [0]. The image below shows a sample of this information:

DataFrame created from NYU’s ‘2014 New York City Neighborhood Names’

Foursquare — ‘Places API’: I will be using Foursquare’s ‘Places API’ to acquire data related to ‘venues’ (as defined by Foursquare) categorized to be somehow associated with music [1]. It is important to note that Foursquare defines a ‘venue’ as a place that one can go to, or check-in to, and that a ‘venue’ is not necessarily a music venue but can be any establishment such as a restaurant or type of retail shop. Each Foursquare ‘venue’ is assigned a ‘category’ and each ‘category’ is associated with a particular ‘categoryID’. The image to the right shows the ‘categoryID’ values provided by Foursquare that will be used to acquire music related venues within New York City:

Foursquare Music-Related Venue CategoryIDs

Data Retrieval

Neighborhood Name & Location Centroid Data: The ‘2014 New York City Neighborhood Names’ dataset hosted by NYU’s Spatial Data Repository was easy to download as a JSON file and import into a Jupyter Notebook:

Importing the newyork_data.json fie

The ‘Borough’, ‘Neighborhood’, ‘Latitude’, and ‘Longitude’ values associated with each neighborhood were then converted from JSON to a Pandas DataFrame that serves as the foundation of the analysis.

Creating a DataFrame out of the JSON data

Foursquare Music-Related Venue Data: As mentioned in the Data Sources section of this report, Foursquare has numerous ‘Venue Categories’ that are used to identify each type of venue. A ‘get’ request to the ‘api.foursquare.com/v2/venues/search?’ endpoint that provides a category ID will return venues of that category. The example code below sends a ‘get’ request to Foursquare that asks for one venue with the “Music Store’ category (categoryID = ‘4bf58dd8d48988d1fe941735’):

Example Foursquare Places API Request

A preliminary dataset of music related venues associated with each New York City neighborhood was created by recursively sending ‘get’ requests to the previously mentioned endpoint, making sure the results are specific to venues with music related ‘category IDs’. For each neighborhood, we can include all of the selected category IDs in a single ‘get’ request by passing them as comma separated values. Shown below is a function that creates the required url and an example:

Dynamically creating API request URLs

The following function recursively sends a ‘get’ request to Foursquare for each neighborhood that requests all music related venues. While looping through each neighborhood from the NYU dataset, the function appends each music related venue entry to a list and, after looping through each neighborhood, creates a DataFrame of all of the results. Included for each entry in the dataset are neighborhood name and location, and venue name, location, and category.

Recursively retrieving music-related venues for each New York City neighborhood

The resulting preliminary venue DataFrame includes 9,442 venues that were pulled from Foursquare:

9,442 venues were pulled from Foursquare

Since I had some issues with exceeding Foursquare’s API rate limits, after the preliminary dataset was acquired, a copy was saved to csv so that future development would not require re-requesting information from Foursquare.

Write data to csv