Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
运行Python Choropleth代码时出现JSON解码错误_Python_Choropleth - Fatal编程技术网

运行Python Choropleth代码时出现JSON解码错误

运行Python Choropleth代码时出现JSON解码错误,python,choropleth,Python,Choropleth,重新发布,因为我以前的帖子不清楚 我正在使用下面的链接帮助创建本地邮政编码中阳性测试的热图。我有一个数据框“Combined_POS2”,看起来像这样(它包含20多个独特的邮政编码): 我想制作一张热图,其中颜色由邮政编码边界决定(如链接中的州示例,但适用于本地邮政编码) 这是我正在使用的Json文件 我正在运行以下代码: import pandas as pd import numpy as np import folium from folium import plugins %matplo

重新发布,因为我以前的帖子不清楚

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

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

这是我正在使用的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
---------------------------------------------------------------------------
TypeError回溯(最近一次调用上次)
在里面
10填充不透明度=0.7,
11线不透明度=0.2,
--->12图例_name='失业率(%)'
13).将_添加到(m)
14
中的~\AppData\Local\Continuum\anaconda3\lib\site packages\folium\features.py
__初始化(自身、地理位置数据、数据、列、按键、箱子、填充颜色、,
nan_填充颜色、填充不透明度、nan_填充不透明度、线条颜色、线条权重、,
线条不透明度、名称、图例名称、覆盖、控件、显示、拓扑,
平滑系数,高光,**kwargs)
1249 style_函数=style_函数,
1250平滑系数=平滑系数,
->1251 highlight_function=如果高亮显示,则高亮显示_function
其他(无)
1252
1253 self.add_child(self.geojson)
中的~\AppData\Local\Continuum\anaconda3\lib\site packages\folium\features.py
__初始化函数(self、数据、样式函数、突出显示函数、名称、覆盖、,
控件、显示、平滑因子、工具提示、嵌入、弹出)
454
455如果是self.style或self.highlight:
-->456 self.convert_to_feature_collection()
457如果自我风格:
458自我验证功能(样式功能,“样式功能”)
中的~\AppData\Local\Continuum\anaconda3\lib\site packages\folium\features.py
将\u转换为\u功能\u集合(自)
501 def转换为功能集合(自):
502“如果尚未将数据转换为FeatureCollection,则将其转换为FeatureCollection。”
-->503如果self.data['type']=='FeatureCollection':
504返回
505如果不是自嵌入:
TypeError:列表索引必须是整数或片,而不是str
我在网上找到了这个资源,但老实说,我对Python和Json是如此的陌生(我的脑袋里满是可用的材料),我不知道从哪里开始尝试解决我的问题


您正试图使用不合适的地理数据绘制choropleth地图

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

此外,你似乎想画一张热图,而不是一张笑脸。在这种情况下,试着阅读


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

您可以写“这是我正在使用的Json文件…”,但您的代码使用了不同的URL。如果您将代码更改为使用此URL会发生什么情况?链接是原始文件-我在这里找到了数据是的,但您的代码没有尝试加载原始JSON文件(这似乎是您想要的),而是尝试在您的注释中加载URL(返回HTML页面)并尝试解析此HTML,就像它是JSON一样。把
raw.githubusercontent.com
URL放在
URL
变量的值中,看看这是否有变化。啊-我现在跟着-代码与上面的代码相同,我现在希望我改变了:“URL=”“state\u geo=f'{URL}/USCities.json”。我没有收到一个错误,上面写着“TypeError:列表索引必须是整数或切片,而不是str”。请编辑您的问题,以(a)更新代码,并(b)包含此新错误消息的完整回溯。我现在明白了-我有代码创建混合热图,就像此链接中的最后两个贴图一样。然而,我想创建一个类似于此链接中代码单元14中详细说明的映射,它使用json数据和Choropleth方法。很抱歉,我把事情弄糊涂了。@Raven:在这种情况下,您必须找到邮政编码边界的GeoJson数据。试着在你所拥有的邮政编码周围找到合适的数据:你链接到的文件中有40000个邮政编码,而这些邮政编码太多了,无法在ChoroPlath上显示。
  ---------------------------------------------------------------------------
    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