Creating a Choropleth From Data
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.
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.
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.
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.
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.
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
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.
Become a Member to join the conversation.