Issues with Folium Choropleth Mapping Example

╄→гoц情女王★ 提交于 2019-12-10 11:26:55

问题


I am trying to create a choropleth map using folium, following the example here: https://pypi.python.org/pypi/folium. The goal is to produce a choropleth map of US unemployment rates, but when I open my map US states are not shaded in. Any suggestions?

import folium
import pandas as pd

state_geo = r'data/us-states.json'
state_unemployment = r'data/US_Unemployment_Oct2012.csv'

state_data = pd.read_csv(state_unemployment)

#Let Folium determine the scale
map = folium.Map(location=[48, -102], zoom_start=3)
map.geo_json(geo_path=state_geo, data=state_data,
             columns=['State', 'Unemployment'],
             key_on='feature.id',
             fill_color='YlGn', fill_opacity=0.7, line_opacity=0.2,
             legend_name='Unemployment Rate (%)')
map.create_map(path='us_states.html')

Thanks,


回答1:


I think the issue is a mis-match of column names:

1) data=state_data has columns ['State', 'Unemployment'] 2) if you open us-states.json, you will find key_on='feature.id' corresponds to '01', '02' and so on..

In folium key_on is suppose to match the first column of data, in this case 'State'.

But '01', '02' ..doesn't fit 'State' column which has 'AL', 'AK', 'AZ'..

If you could come up with key_on in us-states.json that matches 'States' columns, I think it should solve your problem.

Note: I am assuming us-states.json is from https://raw.githubusercontent.com/alignedleft/d3-book/master/chapter_12/us-states.json and US_Unemployment_Oct2012.csv from https://raw.githubusercontent.com/python-visualization/folium/master/examples/US_Unemployment_Oct2012.csv



来源:https://stackoverflow.com/questions/34295540/issues-with-folium-choropleth-mapping-example

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!