使用大叶草绘制特定国家/地区的世界choropleth地图时出错

时间:2019-10-13 17:38:07

标签: 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=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')

其中 countries.csv 如下:

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"

有什么解决方案可以使用其名称或三个字母代码(例如美国的美利坚合众国)为特定国家制作 choropleth 地图吗?

1 个答案:

答案 0 :(得分:0)

您的列应该是“国家”,而不是“国家_0”