Search code examples
pythonfolium

Folium chloropleth map only showing grey - help to troubleshoot


I having trouble getting some air pollution data to show different colors in a chloropleth map using folium. Please let me know where my code may be throwing an error. I think it is the key_on parameter but need help.

This is how my map turns out. enter image description here

What I would like is for the mean concentration of the air pollution data to show up on the map but the map is still greyed out.

Here are the files I used:

  1. Geojson file - Used "download zip" in upper right of this website https://gist.github.com/miguelpaz/edbc79fc55447ae736704654b3b2ef90#file-uhf42-geojson

  2. Data file - Exported data from here https://a816-dohbesp.nyc.gov/IndicatorPublic/VisualizationData.aspx?id=2023,719b87,122,Summarize

    Here is my code:

import geopandas as gpd import folium

#clean pollution data
pm_df1 = pd.read_csv('/work/Fine Particulate Matter (PM2.5).csv',header = 5, usecols = ['GeoTypeName', 'Borough','Geography', 'Geography ID','Mean (mcg per cubic meter)'], nrows = 140)

#limit dataframe to rows with neighborhood (UHF 42) that matches geojson file
pm_df2 = pm_df1[(pm_df1['GeoTypeName'] == 'Neighborhood (UHF 42)')]
pm_df2

#clean geojson file
uhf_df2 = gpd.read_file('/work/uhf42.geojson', driver='GeoJSON')
uhf_df2.head()

#drop row 1 that has no geography
uhf_df3 = uhf_df2.iloc[1:]
uhf_df3.head()

## create a map
pm_testmap = folium.Map(location=[40.65639,-73.97379], tiles = "cartodbpositron", zoom_start=10)

# generate choropleth map 
pm_testmap.choropleth(
    geo_data=uhf_df3,
    data=pm_df2,
    columns=['Geography', 'Mean (mcg per cubic meter)'],
    key_on='feature.properties.uhf_neigh',  #think this is where I mess up.
    fill_color='BuPu', 
    fill_opacity=0.2, 
    line_opacity=0.7,
    legend_name='Average dust concentration',
    smooth_factor=0)

# display map
pm_testmap

Solution

  • The problem with key_on is right as you think.
    Both data have the name of UHF written on them, but in a completely different form.
    In order to link these two, the data must first be preprocessed. I don't know your data. It would be nice if you could df.head() the two data to show them, but I'll explain based on the data I checked through the link you provided.

    In your geojson file, uhf_neigh simply says Northeast Bronx. However, your PM data appears to have the region listed as Bronx: Northeast Bronx. The following process seems to be necessary to unify your local name before plotting map.

    uhf_df2['UHF_NEIGH'] = uhf_df2['BOROUGH']+ ': ' + uhf_df2['UHF_NEIGH']