Multiple Map Points Using Folium

You won’t always have GPS coordinates in your data set. A frequent use case might be that you have a list of addresses that correspond to other features of interest. Imagine having a list of crime locations, or addresses of people infected with COVID or rental vacancies. In order to plot these points graphically to convey something meaningful, you'll have to get the coordinates. There are different ways of doing this. Since you’ll frequently begin with a csv or spreadsheet, we’ll take a look at a relatively simple way you can do this.

As I’m sure you know, you can upload a CSV or excel spreadsheet into a google sheet. Since you can do this, we can take advantage of google’s integration with other applications, in this case, google maps. So I’ll start by uploading a simple spreadsheet with two columns, company name and address.

Once you’ve uploaded your data, you can actually add a few lines of code to make a custom-made function to call upon google maps. I can’t claim to have written this myself; it’s available on Looker, a data analytics company acquired by Google. You can write scripts by clicking tools in the tool bar at the top. A menu will drop down; click where it says script editor:

When you’ve done so, a script editor will open in another tab:

An untitled project will open with an empty function in the script editor. If you go to the Looker community board link here, you’ll find specific instructions along with the javascript code you'll need to call on google maps from google sheets. Paste that into the script editor and save:

Great. You now have a function called GEOCODE_GOOGLE created in the script editor that is callable from google sheets taking one argument called “address”. You can now use that in google sheets like any other function:

Copy and paste that formula into all the cells below. It will take a moment to load, but once it’s done, you’ll have the GPS coordinates:

You can now download that into a csv or excel file so that you can use in your jupyter notebook for plotting the locations on a map.

Here I split the GPS coordinates into two columns for Latitude and Longitude as it was earlier to work with in this format. Obviously, any other preprocessing would have to be accomplished at this stage such as addressing null values but my set was pretty clean so aside from splitting the columns, I was already set.

I’ll need one last set of coordinates before I can plot the map: the coordinates I gathered earlier was for individual addresses. I looked at the addresses and saw that they were throughout the United States so I needed a map of the whole country. Now, that’s not an address so I coldn’t do this in the previous step with the other coordinates. Fortunatley, I previously discovered a site called Latlong where you can input an address, city ostate or country, and it will return the GPS coordinates:

Now back to our Jupyter notebook. As I showed in a previous blog, there are different map styles (called tiles in Folium) you can choose from depending on your taste. I like the black & White Film Noir feel of one called “Stamen Toner” So that’s the one I’ll use. aside from that, I’m using two other parameters: location which is the GPS coordinate set, and zoom_start which is closely or distantly you wish to zoom in. I had to play around with this to get it just right but ultimately used a zoom_start setting of 4. So this is how it turned out:

Doesn’t that look cool? Obviously, your tile choices would be dependent upon your use case so you’ll definitely want to play around here.

Now, we can loop through our data frame for the column values we need (“Lat” and “Long”) for each row in our dataframe. We’ll use a method called “Marker” available in the folium library to create individual markers. Here I’m using three parameters to define the markers that I’ll then add to the map” Latitude, Longitude, and popup. Popup will add a popup of the company when we hover and click on a marker.

So that’s it! We’ve now collected a group of GPS coordinates in a batch and added markers for those coordinates to our map with a popup of the company name for each marker. Pretty useful, huh?