【发布时间】:2020-06-12 19:08:33
【问题描述】:
因为我之前的帖子不清楚,所以重新发布。
我正在使用下面的链接来帮助创建本地邮政编码中阳性测试的热图。我有一个数据框“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
【问题讨论】:
-
您写“这是我正在使用的 Json 文件...”但您的代码使用了不同的 URL。如果您更改代码以使用此 URL,会发生什么情况?
-
链接是原始文件-我在这里找到了数据github.com/millbj92/US-Zip-Codes-JSON/blob/master/USCities.json
-
是的,但您的代码并没有尝试加载原始 JSON 文件(您似乎想要这样做),而是尝试在您的评论中加载 URL(返回 HTML 页面)和试图解析这个 HTML,就好像它是 JSON 一样。将
raw.githubusercontent.comURL 放入url变量的值中,看看是否会发生变化。 -
啊 - 我现在跟着 - 代码和上面一样现在期望我改变了: "url = 'raw.githubusercontent.com/millbj92/US-Zip-Codes-JSON/master'" "state_geo = f'{url}/USCities.json'" 。我没有收到错误提示“TypeError:列表索引必须是整数或切片,而不是 str”。
-
请编辑您的问题以 (a) 更新您的代码并 (b) 包含此新错误消息的完整回溯。
标签: python choropleth