Get startedGet started for free

Creating markers and popups in folium

1. Creating markers and popups in folium

You can add markers to a folium map to show point locations and you can use popups with those markers to relay information about what is located at each point.

2. Using iterrows()

We used the pandas iterrows() generator to create a dictionary earlier in this lesson. We'll use iterrows() again to build markers for a folium map, so let's take a closer look at how it works. Iterrows returns a tuple for each iteration: the first value is the row index and the second value contains the row values. In this for-loop, we get the second value of the row iterator, store it as row_values, and print it each time we iterate through the schools_in_dist1 GeoDataFrame. Here is the result of the first two iterations.

3. Building marker locations

We can make use of iterrows() to build our marker locations. With each iteration, we'll get the latitude and longitude from row_values in order to construct a location for each marker. While we are still in the for-loop, we can use the add_to() method of marker to add the marker to the district1_map. When we have finished iterating through the schools_in_dist1 GeoDataFrame, we exit the loop and display the map.

4. Building marker locations

Here is the result. We get a map originating from the center of school district one along with a marker for each school in that district. Next, we'll add popups to the markers for even more information about the schools in district one.

5. Creating popups from data in a DataFrame

Here is the same for loop as before, with two differences. We are creating a popup on each iteration and we are assigning that popup to the popup parameter with each marker construction. You can chose any value from a DataFrame to construct the popup. And because the string assigned to a popup renders as HTML, you can add HTML tags to style it. In this example we have used the <strong> html tags to make the popup text a bold-faced font. I wanted to mention that the popup strings render as HTML, but we won't be spending time on it in the exercises.

6. Creating popups from data in a DataFrame

And here is the result. We have the school district one map as before, but when we click on a marker, we now see the school name in a bold-faced font.

7. Troubleshooting popups

You may need to clean up some of the data in your DataFrame in order to make your popups as clean and informative as they can be. Two common issues that you may encounter are missing values and single or double quotes in the data itself. Here we see an example of the first problem. In the upcoming exercise, you will see examples of both of these issues.

8. Let's practice!

Now that you know how to add markers and popups to your folium map, it's time to go practice those skills.