在Python上使用Folium尝试执行Choropleth映射时出错

时间:2019-01-15 11:15:32

标签: python dictionary choropleth folium

我一直在使用Folium Choropleth地图制作一张显示旧金山不同社区犯罪率的地图。

我正在使用的geojson文件是: https://cocl.us/sanfran_geojson,我将其保存为“数据”

我正在使用的数据来自: https://cocl.us/sanfran_crime_dataset,我将其保存为“ cdata”。

我的代码如下;

cdata.rename(columns={'PdDistrict':'Neighbourhood'}, inplace=True)

neighbourhood = cdata.groupby(['Neighbourhood']).size().reset_index(name='Count')

获得:

  Neighbourhood     Count
0   BAYVIEW         14303
1   CENTRAL         17666
2   INGLESIDE       11594
3   MISSION         19503
4   NORTHERN        20100
5   PARK            8699
6   RICHMOND        8922
7   SOUTHERN        28445
8   TARAVAL         11325
9   TENDERLOIN      9942

然后我用:

sanfran = folium.Map(location=[37.7749, -122.4194], zoom_start = 12)
sanfran.choropleth(
    geo_data=dat,
    name='choropleth',
    data=neighbourhood,
    columns=['Neighbourhood', 'Count'],
    key_on='properties.DISTRICT',
    fill_color='YlOrRd',
    fill_opacity = 0.7,
    line_opacity=0.2,
    legend_name='Crime Rate in San Francisco')

运行sanfran.choropleth代码时,出现以下错误:

C:\ProgramData\Anaconda3\lib\site-packages\folium\folium.py:432: FutureWarning: The choropleth  method has been deprecated. Instead use the new Choropleth class, which has the same arguments. See the example notebook 'GeoJSON_and_choropleth' for how to do this.
FutureWarning

然后,如果我只是键入“ sanfran”并运行代码,我将得到:

unhashable type: 'list'

非常感谢您的帮助,谢谢!

2 个答案:

答案 0 :(得分:0)

语法有所更改。

在大叶上以大写字母“ C”调用Choropleth,并通过链“ add_to(map)”方法传递地图名称,就像这样

folium.Choropleth(all your arguments as the old method).add_to(map)

选中此链接,然后向下滚动至Choropleth地图上的部分以获取详细信息。 https://python-visualization.github.io/folium/quickstart.html#Getting-Started

答案 1 :(得分:0)

如果要运行并创建地图,则应遵循folium的新语法。 您的代码应如下所示:

sanfran = folium.Map(location=[37.7749, -122.4194], zoom_start=12)
folium.Choropleth(
    geo_data=dat,
    name='choropleth',
    data=neighbourhood,
    columns=['Neighbourhood', 'Count'],
    key_on='properties.DISTRICT',
    fill_color='YlOrRd',
    fill_opacity=0.7,
    line_opacity=0.2,
    legend_name='Crime Rate in San Francisco').add_to(sanfran)

sanfran.save('outfile.html')