我正在尝试按照本教程创建地图。我使用与教程中几乎完全相同的代码,但是得到一个系列我不确定如何开始解决,有人对我如何开始解决有任何想法吗?
下面是我的代码。我认为我的表格合并工作正常,因为它在我打印表格时显示正确,但我认为使用 choropleth 函数开始走下坡路。
'''
import geopandas
import pandas as pd
import folium
url = 'https://en.wikipedia.org/wiki/List_of_countries_by_meat_consumption'
tables = pd.read_html(url)
table = tables[0]
world = geopandas.read_file(geopandas.datasets.get_path('naturalearth_lowres'))
table = world.merge(table, how='left', left_on=['name'], right_on=['Country'])
pd.options.display.width = 4000
pd.set_option('display.max_rows', 188)
table = table.dropna(subset=['kg/person (2002)[9][note 1]'])
meat_map = folium.Map()
folium.Choropleth(
geo_data=table,
name='choropleth',
data=table,
columns=['Country', 'kg/person (2002)[9][note 1]'],
key_on ='feature.properties.name',
fill_color ='YlGnBu',
fill_opacity = 0.7,
line_opacity = 0.2,
legend_name = "Meat consumption in kg/person"
).add_to(meat_map)
这是我的错误信息:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-11-b9be599a18fa> in <module>
10 meat_map = folium.Map()
11
---> 12 folium.Choropleth(
13 geo_data=table,
14 name='choropleth',
~\anaconda3\envs\geo_env\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)
1287 smooth_factor=smooth_factor)
1288 else:
-> 1289 self.geojson = GeoJson(
1290 geo_data,
1291 style_function=style_function,
~\anaconda3\envs\geo_env\lib\site-packages\folium\features.py in __init__(self, data, style_function, highlight_function, name, overlay, control, show, smooth_factor, tooltip, embed, popup, zoom_on_click, marker)
497 self.marker = marker
498
--> 499 self.data = self.process_data(data)
500
501 if self.style or self.highlight:
~\anaconda3\envs\geo_env\lib\site-packages\folium\features.py in process_data(self, data)
540 if hasattr(data, 'to_crs'):
541 data = data.to_crs('EPSG:4326')
--> 542 return json.loads(json.dumps(data.__geo_interface__))
543 else:
544 raise ValueError('Cannot render objects with any missing geometries'
~\anaconda3\envs\geo_env\lib\json\__init__.py in dumps(obj, skipkeys, ensure_ascii, check_circular, allow_nan, cls, indent, separators, default, sort_keys, **kw)
229 cls is None and indent is None and separators is None and
230 default is None and not sort_keys and not kw):
--> 231 return _default_encoder.encode(obj)
232 if cls is None:
233 cls = JSONEncoder
~\anaconda3\envs\geo_env\lib\json\encoder.py in encode(self, o)
197 # exceptions aren't as detailed. The list call should be roughly
198 # equivalent to the PySequence_Fast that ''.join() would do.
--> 199 chunks = self.iterencode(o, _one_shot=True)
200 if not isinstance(chunks, (list, tuple)):
201 chunks = list(chunks)
~\anaconda3\envs\geo_env\lib\json\encoder.py in iterencode(self, o, _one_shot)
255 self.key_separator, self.item_separator, self.sort_keys,
256 self.skipkeys, _one_shot)
--> 257 return _iterencode(o, 0)
258
259 def _make_iterencode(markers, _default, _encoder, _indent, _floatstr,
~\anaconda3\envs\geo_env\lib\json\encoder.py in default(self, o)
177
178 """
--> 179 raise TypeError(f'Object of type {o.__class__.__name__} '
180 f'is not JSON serializable')
181
TypeError: Object of type MultiPolygon is not JSON serializable
'''