简体   繁体   English

使用 folium 为特定国家/地区绘制世界等值线 map 时出错

[英]Error plotting world choropleth map for specific countries using folium

I am trying to plot choropleth map of specific countries using python folium as given below:我正在尝试使用 python folium 的特定国家/地区的 plot choropleth map,如下所示:

#!/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')

where countries.csv is given below:其中country.csv如下所示:

Country                 
United States of America 
United Kingdom              
France                      
Canada                      
Japan                       
Italy                       
Germany                     
Russia                      
India                       
Spain                       
Australia                   
Hong Kong                   
South Korea

when I am trying to compile this code I am getting this error which is shown below:当我尝试编译此代码时,我收到如下所示的错误:

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"

Is there any solution that I can make choropleth map for the specific countries using their names or their three letter codes like US for United States of America?有没有什么解决方案可以让我使用特定国家/地区的名称或他们的三个字母代码(例如 US 代表美国)为特定国家/地区制作choropleth map?

Your column should be 'Country', not 'Country_0'您的列应该是“Country”,而不是“Country_0”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM