Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Creating a Choropleth From Data

00:00 In this lesson, you’ll create a choropleth in Folium based on a separate dataset.

00:06 To begin, a choropleth is a map that uses color or shading to represent data. You can build choropleth maps for one specific region or for the entire world.

00:18 Either way, the colors of your map will convey quantitative information such as the average elevation or the number of lakes in each area. So let’s use Folium to make a choropleth map for the population of New York City.

00:35 You can use the pandas library to easily create a population dataset and apply it to your Folium map. Import the pandas library and alias it as pd.

00:46 This code allows you to create a pandas DataFrame called borough_population with the populations of each NYC borough according to the 2020 US census.

01:00 This DataFrame has five rows, one row for each borough, and two columns. The first column, called borough, contains the name of each borough, like Manhattan and Queens, while the second column, called population, stores the associated populations. For example, Manhattan had about 1.7 million residents as of the 2020 census.

01:25 When making other Folium maps, you might have DataFrames with many more rows or columns, but as we’ll see shortly, the important thing is that you have one column that identifies each region and one column that contains the data you’d like to display.

01:41 You are now ready to add this population data to your NYC map. Recall that in a previous lesson, we loaded in information about the boundaries of each borough through a Geo JSON file.

01:54 We also took a peek at that file and found properties like borough_name. In fact, if you cycle through each of the features in that GeoJSON file and print out that borough_name property, you’ll find names that exactly match the borough names included in the borough_population DataFrame. Folium will be able to create a choropleth based on this population data precisely because these names match.

02:23 When you’re working with your own data, you’ll want to be sure that some identifying property from that GeoJSON file can be matched to the names or codes in your DataFrame. That way, Folium will be able to make a choropleth layer by filling in those shapes from the GeoJSON data. Okay, so let’s go ahead and make a choropleth for the population of NYC.

02:48 You currently have a map that is zoomed in on New York City. To turn this map into a choropleth, you add a choropleth layer to it. Reference the folium.Choropleth, and now you’ll need a few properties.

03:02 First, geo_data = boroughs_file. This should link up the GeoJSON file that contains your boundary data. This could be a file on your computer or a link to a URL with GeoJSON data.

03:18 Then data = borough_population. This argument references your DataFrame.

03:26 Next columns = "borough", "population". This should be a list of two column names coming from your DataFrame: first, the name of the column that has the key that connects to the GeoJSON data, and second, the name of the column that contains the data you want to display.

03:46 And finally, key_on = "feature.properties.borough_name. This will be a string with dot notation specifying the part of the JSON file that links to your DataFrame.

04:01 Note that we have borough_name here, but this will commonly be a different property. It just depends on how your JSON file is structured and where you can find commonalities with your DataFrame. Also, notice that this string is "feature.properties", not "features.properties".

04:22 Even though the properties are found through the features field, this string should still reference a singular feature with no s.

04:31 Once you have those properties in place, you can add this choropleth to your NYC map, and now you’ll see the population of each borough represented as a different shade of blue, with darker blue colors indicating areas with larger populations.

04:47 Blue is the default color palette for Folium choropleth, and Folium has picked default cut points for the buckets of your color bar. You can update both the colors of your map as well as the bins of your color bar, and you’ll learn how to make those updates coming up.

05:05 Up next, you’ll customize your choropleth by updating its colors and including markers for points of interest.

Become a Member to join the conversation.