【问题标题】:JSON Decode Error When Running Python Choropleth Code运行 Python Choropleth 代码时出现 JSON 解码错误
【发布时间】: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.com URL 放入 url 变量的值中,看看是否会发生变化。
  • 啊 - 我现在跟着 - 代码和上面一样现在期望我改变了: "url = 'raw.githubusercontent.com/millbj92/US-Zip-Codes-JSON/master'" "state_geo = f'{url}/USCities.json'" 。我没有收到错误提示“TypeError:列表索引必须是整数或切片,而不是 str”。
  • 请编辑您的问题以 (a) 更新您的代码并 (b) 包含此新错误消息的完整回溯。

标签: python choropleth


【解决方案1】:

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

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

此外,您似乎想绘制热图,而不是等值线。在这种情况下,请尝试阅读this how-to guide

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

【讨论】:

  • 我现在看到了 - 我有代码可以创建一个混合热图,就像此链接 towardsdatascience.com/… 中的最后两张地图一样。但是,我想创建类似于此链接中代码单元 14 中详述的地图,它使用 json 数据和 Choropleth 方法。对不起,如果我混淆了事情。
  • @Raven:在这种情况下,您将不得不为邮政编码边界查找 GeoJson 数据。尝试在您拥有的邮政编码周围找到合适的数据:您链接到的文件中有 40,000 个邮政编码,这太多了,无法在等值线上显示。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-24
  • 1970-01-01
  • 2021-08-05
  • 2018-01-08
相关资源
最近更新 更多