Search code examples
python-3.xgeojsonfolium

Folium choropleth not colorizing ZIP code areas at all


I'm using Folium to generate a revenue report by zip code, to determine where to establish a new office.

While it seems like the geoJSOn is properly overlaying on the map, it doesn't seem to be heat-mapping the revenue amounts to their respective ZIP codes.

The feature name in the JSON file is feature.properties.ZCTA5CE10

As seen here, in the first few lines of the file. ZCTA5CE10 corresponds with the Zipcode. Here is a link to the GeoJSON file on GitHub. https://github.com/OpenDataDE/State-zip-code-GeoJSON/blob/master/tx_texas_zip_codes_geo.min.json

{"type":"FeatureCollection",
"features":[{
  "type":"Feature",
  "properties":
{"STATEFP10":"48","ZCTA5CE10":"75801","GEOID10":"4875801","CLASSFP10":"B5","MTFCC10":"G6350","FUNCSTAT10":"S","ALAND10":555807428,"AWATER10":6484251,"INTPTLAT10":"+31.7345202","INTPTLON10":"-095.5313809","PARTFLG10":"N"},"geometry":{"type":"Polygon","coordinates":[[[-95.680719,31.727999]

My Revenue data is a CSV file with 2 columns, ZIP CODE and AUGUST.

Example CSV:

ZCTA5CE10,AUGUST
"76701",2676.89
"76643",8625.79
"76655",5618
"76710",23265.18
"76708",14618.35
"76706",14335.85
"76705",9338.44
"76633",4215.39
"76712",35488.02
"76657",10186.13
"76664",1361
"76711",2812.35
"76682",713

And finally, my code.

import folium
from folium.plugins import MarkerCluster
import pandas as pd
import os
map = folium.Map(location=[31.5493, -97.1467],
                 default_zoom_start=15)

revdata = pd.read_csv(os.path.join('revenue.csv'))
revdata.info()

folium.Choropleth(geo_data="tx_texas_zip_codes_geo.min.json",
                data = revdata,
                columns = ['ZIP CODE', 'AUGUST'],
                key_on = 'feature.properties.ZCTA5CE10',
                fill_color='BuPu', fill_opacity=0.7, line_opacity = 0.2,
                legend_name = 'REVENUE BY ZIP').add_to(map)
marker_cluster = MarkerCluster().add_to(map)

map.save('mymap.html')

I've tried changing the zip codes in my CSV file to have quotes around them, but that doesn't change anything.

Ideas?


Solution

  • I've realized what the problem is.

    At the point revdata.info I saw that regardless of quote marks on my first column, the column was being imported as an integer type. I updated my CSV read line to revdata = pd.read_csv(os.path.join('revenue.csv'), dtype={'ZCTA5CE10': object}), specifically denoting the first column as needing to be imported as a string literal, which allows it to be correctly compared with the ZIP code in the GeoJSON.

    In addition to this, I used PyGeoJ to clean up my main GeoJSON file to exclude all unused zip codes in my revenue report (which reduced my final map html size from 87mb to 2.3).