
Often when working with data, you have access to geospatial features such as latitude and longitude coordinates. It can be interesting to map these to get an idea of how instances of a data set are spread across different locations.
In this short article, I walk through the steps I took to get bike rental location data and show the locations on a map. I use Dublin Bikes data (https://data.smartdublin.ie/dataset/dublinbikes-api) and the Folium library (https://python-visualization.github.io/folium/), which, as the documentation says, "builds on the data wrangling strengths of the Python ecosystem and the mapping strengths of the leaflet.js
library". For more information on Leaflet, see the docs here.
Requirements
I use Python 3.7, Folium 0.12.1 and Pandas 1.2.4. I also use Jupyter Notebooks in Anaconda Navigator.
The data
The data I am working with is from the Dublin Bikes API, specifically the location of bike rental stations:
https://data.smartdublin.ie/dataset/33ec9fe2-4957-4e9a-ab55-c5e917c7a9ab/resource/2dec86ed-76ed-47a3-ae28-646db5c5b965/download/dublin.csv
Step 1 – Importing Pandas and Folium
The first step, as always, is importing the libraries:
import pandas as pd
import folium
Step 2 – Getting the bike station locations and saving to a Pandas data frame
Using Pandas I go direct to the location of the CSV file I want:
location = "https://data.smartdublin.ie/dataset/33ec9fe2-4957-4e9a-ab55-c5e917c7a9ab/resource/2dec86ed-76ed-47a3-ae28-646db5c5b965/download/dublin.csv"
bike_station_locations = pd.read_csv(location)
Now I have the bike_station_locations dataframe:

Step 3 – Keeping only the columns I need
I want to keep the Latitude, Longitude and the Name of the location. The former two columns will allow me to map the locations and the latter I will use to give each location pin a name:
bike_station_locations = bike_station_locations[["Latitude", "Longitude", "Name"]]
Step 4 – Creating the map
For the map, the first step is to create a map of the location I want. Using the location parameter, I pass in the mean of the latitude and longitude coordinates I have to centre the map there.
map = folium.Map(location=[bike_station_locations.Latitude.mean(), bike_station_locations.Longitude.mean()], zoom_start=14, control_scale=True
This gives a blank map centered on the location previously given and zoomed to 14.

Step 5 – Adding points to the map
Now I add the points for each bike station location to the map. Iterating through each row of the dataframe, I pass the location latitude and longitudes to folium.Marker as a list and pass the name to the popup parameter. And for each location I add to map:
for index, location_info in bike_station_locations.iterrows():
folium.Marker([location_info["Latitude"], location_info["Longitude"]], popup=location_info["Name"]).add_to(map)
I then display the map:

Note: The maps use an OpenStreetMap tile by default, though other options can be used by adding the parameter tiles parameter and value when creating the map. There are examples in the docs here.
For more information on OpenStreetMap, can be found here.
Conclusion
Using Folium with Python is a great way to get started with mapping Geospatial data. With a few lines of code, it is possible to put together basic maps. There are many other options available, and the documentation is well worth exploring.
Jupyter Notebook available here.
All screenshots were taken by the author. Introductory image by Calvin Hanson on Unsplash.