首页 > 解决方案 > 运行 Python Choropleth 代码时出现 JSON 解码错误

问题描述

由于我之前的帖子不清楚,所以重新发布。

我正在使用下面的链接来帮助创建本地邮政编码中阳性测试的热图。我有一个数据框“Combined_POS2”,它看起来像这样(它包含 20 多个唯一的邮政编码):

 Zip Code      Count
 21216         45
 21210         24
 21230         30

我想制作一个热图,其中颜色由邮政编码边界划定(如链接中的州示例,但适用于本地邮政编码)。

这是我正在使用的 Json 文件https://raw.githubusercontent.com/millbj92/US-Zip-Codes-JSON/master/USCities.json

我正在运行以下代码:

import pandas as pd
import numpy as np
import folium
from folium import plugins
%matplotlib inline
import geojson
import geopandas as gpd

Combined_POS2 = Combined_POS1['ZIP'].value_counts().rename_axis('ZIP').reset_index(name='counts')

url = 'https://raw.githubusercontent.com/millbj92/US-Zip-Codes-JSON/master'
state_geo = f'{url}/USCities.json'

m = folium.Map(location=[48, -102], zoom_start=3)

folium.Choropleth(
geo_data=state_geo,
name='choropleth',
data=Combined_POS2,
columns=['ZIP', 'counts'],
key_on='feature.zip_code',
fill_color='YlGn',
fill_opacity=0.7,
line_opacity=0.2,
legend_name='Unemployment Rate (%)').add_to(m)

folium.LayerControl().add_to(m)

当我运行此代码时,我收到此错误:

  ---------------------------------------------------------------------------
    TypeError                                 Traceback (most recent call last)
<ipython-input-290-fecdd8605e8f> in <module>
 10     fill_opacity=0.7,
 11     line_opacity=0.2,
 ---> 12     legend_name='Unemployment Rate (%)'
 13 ).add_to(m)
 14 

 ~\AppData\Local\Continuum\anaconda3\lib\site-packages\folium\features.py in 
 __init__(self, geo_data, data, columns, key_on, bins, fill_color, 
 nan_fill_color, fill_opacity, nan_fill_opacity, line_color, line_weight, 
 line_opacity, name, legend_name, overlay, control, show, topojson, 
 smooth_factor, highlight, **kwargs)
 1249                 style_function=style_function,
 1250                 smooth_factor=smooth_factor,
 ->  1251                 highlight_function=highlight_function if highlight 
 else None)
 1252 
 1253         self.add_child(self.geojson)

 ~\AppData\Local\Continuum\anaconda3\lib\site-packages\folium\features.py in 
 __init__(self, data, style_function, highlight_function, name, overlay, 
 control, show, smooth_factor, tooltip, embed, popup)
454 
455         if self.style or self.highlight:
 --> 456             self.convert_to_feature_collection()
457             if self.style:
458                 self._validate_function(style_function, 'style_function')

 ~\AppData\Local\Continuum\anaconda3\lib\site-packages\folium\features.py in 
convert_to_feature_collection(self)
501     def convert_to_feature_collection(self):
502         """Convert data into a FeatureCollection if it is not already."""
 --> 503         if self.data['type'] == 'FeatureCollection':
504             return
505         if not self.embed:

TypeError: list indices must be integers or slices, not str

我在网上找到了这个资源,但老实说,我对 Python 和 Json 还很陌生(我的头在旋转所有可用的材料),我不知道从哪里开始尝试找出我的问题。https://github.com/python-visualization/folium/issues/918

https://python-visualization.github.io/folium/quickstart.html#Getting-Started

标签: pythonchoropleth

解决方案


您正在尝试使用不合适的地理数据绘制等值线图。

要绘制等值线图,您需要GeoJson数据,但您提供的邮政编码列表不是 GeoJson。它似乎包含每个邮政编码的单个经纬度点。此文件中没有您提到的邮政编码边界。

此外,您似乎想绘制热图,而不是等值线。在这种情况下,请尝试阅读此操作指南

如果 Folium 能够指出错误并说“抱歉,这些数据不是我们需要的”,而不是在稍后某个时间点出现模糊错误而失败,那就太好了。


推荐阅读