Creating a Simple Map with Folium and Python | by Liam Connors | Towards Data Science

Often when working with data you will 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”. More information on Leaflet can be found in the docs here.
I used Python 3.7, Folium 0.12.1 and Pandas 1.2.4. I also use Jupyter Notebooks in Anaconda Navigator.
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
There is lots of other interesting data available in relation to Dublin Bikes data, including data on bike usage. Something I would like to explore in the future.
The first step, as always, is importing the libraries:
import pandas as pdimport folium
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:
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"]]
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.
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.
More information on OpenStreetMap can be found here.
Using Folium with Python is a great way to get stared 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 taken by the author. Introductory image by Calvin Hanson on Unsplash.