Python 使用folium绘制特定国家的世界choropleth地图时出错
我试图使用python folium绘制特定国家的choropleth地图,如下所示:Python 使用folium绘制特定国家的世界choropleth地图时出错,python,data-visualization,folium,Python,Data Visualization,Folium,我试图使用python folium绘制特定国家的choropleth地图,如下所示: #!/usr/bin/python3 import pandas as pd import os import folium country_count = pd.read_csv('countries.csv') country_geo = os.path.join('world-countries.json') m = folium.Map(location=[0, 0], zoom_start=
#!/usr/bin/python3
import pandas as pd
import os
import folium
country_count = pd.read_csv('countries.csv')
country_geo = os.path.join('world-countries.json')
m = folium.Map(location=[0, 0], zoom_start=2)
m.choropleth(
geo_data=country_geo,
name='choropleth',
data=country_count,
columns=['Country_0'],
key_on='feature.properties.name',
fill_color='YlGn',
fill_opacity=0.7,
line_opacity=0.2,
legend_name='Test'
)
folium.LayerControl().add_to(m)
m.save('#292_folium_chloropleth_country.html')
Country
United States of America
United Kingdom
France
Canada
Japan
Italy
Germany
Russia
India
Spain
Australia
Hong Kong
South Korea
其中countries.csv如下所示:
#!/usr/bin/python3
import pandas as pd
import os
import folium
country_count = pd.read_csv('countries.csv')
country_geo = os.path.join('world-countries.json')
m = folium.Map(location=[0, 0], zoom_start=2)
m.choropleth(
geo_data=country_geo,
name='choropleth',
data=country_count,
columns=['Country_0'],
key_on='feature.properties.name',
fill_color='YlGn',
fill_opacity=0.7,
line_opacity=0.2,
legend_name='Test'
)
folium.LayerControl().add_to(m)
m.save('#292_folium_chloropleth_country.html')
Country
United States of America
United Kingdom
France
Canada
Japan
Italy
Germany
Russia
India
Spain
Australia
Hong Kong
South Korea
当我试图编译这段代码时,我遇到如下错误:
Traceback (most recent call last):
File "choropleth1.py", line 20, in <module>
legend_name='Test'
File "/home/ammar/.local/lib/python3.7/site-packages/folium/folium.py", line 418, in choropleth
self.add_child(Choropleth(*args, **kwargs))
File "/home/ammar/.local/lib/python3.7/site-packages/folium/features.py", line 1063, in __init__
color_data = data.set_index(columns[0])[columns[1]].to_dict()
File "/home/ammar/.local/lib/python3.7/site-packages/pandas/core/frame.py", line 4396, in set_index
raise KeyError("None of {} are in the columns".format(missing))
KeyError: "None of ['Country'] are in the columns"
回溯(最近一次呼叫最后一次):
文件“choropleth1.py”,第20行,在
图例_name='Test'
文件“/home/ammar/.local/lib/python3.7/site packages/folium/folium.py”,第418行,以choropleth格式
self.add_child(Choropleth(*args,**kwargs))
文件“/home/ammar/.local/lib/python3.7/site packages/folium/features.py”,第1063行,在__
color_data=data。将_索引(列[0])[列[1]]设置为_dict()
文件“/home/ammar/.local/lib/python3.7/site packages/pandas/core/frame.py”,第4396行,在set_索引中
raise KeyError(“列中没有任何{}”。格式(缺少))
KeyError:“['Country']都不在列中”
是否有任何解决方案,我可以使用特定国家的名称或三个字母代码(如美国)为特定国家绘制地图?您的栏应该是“国家”,而不是“国家”