python - 运行 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
解决方案
推荐阅读
- statistics - 当预测与事实之间的相关性为正时,r-squared 如何为负?
- windows - 使用自签名证书时,在 windows 上签署电子生成器生成的包失败
- numpy - 如何从分类分布中抽取样本
- reactjs - Typescript 中的类型 XXX 上不存在属性“forEach”
- html - 避免 col 内的元素重叠?
- flutter - RenderRepaintBoundary 对象在布局期间被赋予了无限大小
- python - 功能修改原图
- python - 无法安装lxml
- python - python的初学者问题:如何在python的文件中制作一个分隔行列表
- php - 在 Woocommerce 中按星级 ASC 添加产品排序选项