我在Jupyter Notebook上运行带有FOlium的代码,以便为意大利创建地图。我的问题是我做不好。 CSV位于我的数据文件夹中,并且具有如下csv:在将geojson导入工具时,我没有成功。数据很简单,只有5列,我需要在每个城市中输入一些数字来显示。使用此链接进行研究:http://python-visualization.github.io/folium/docs-v0.5.0/quickstart.html#Choropleth-maps
Rank City 2011 2017 Region
1 Rome 2617175 2872800 Lazio
下面是我的代码:
import pandas as pd
import os
state_geo = os.path.join('data', 'municipalities.topojson')
state_unemployment = os.path.join('data', 'italy.csv')
state_data = pd.read_csv(state_unemployment)
m = folium.Map(location=[41.8719, 12.5674], zoom_start=3)
m.choropleth(
geo_data=state_geo,
name='choropleth',
data=state_data,
columns=['City', '2017'],
key_on='feature.City',
fill_color='YlGn',
fill_opacity=0.7,
line_opacity=0.2,
legend_name='Unemployment Rate (%)'
)
folium.LayerControl().add_to(m)
m
我遇到这个错误:
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
/usr/local/lib/python3.6/site-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
3062 try:
-> 3063 return self._engine.get_loc(key)
3064 except KeyError:
pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()
pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()
pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()
pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()
KeyError: 'City'
During handling of the above exception, another exception occurred:
KeyError Traceback (most recent call last)
<ipython-input-60-968901ace66d> in <module>()
18 fill_opacity=0.7,
19 line_opacity=0.2,
---> 20 legend_name='Unemployment Rate (%)'
21 )
22
/usr/local/lib/python3.6/site-packages/folium/folium.py in choropleth(self, geo_data, data, columns, key_on, threshold_scale, fill_color, fill_opacity, line_color, line_weight, line_opacity, name, legend_name, topojson, reset, smooth_factor, highlight)
250 if hasattr(data, 'set_index'):
251 # This is a pd.DataFrame
--> 252 color_data = data.set_index(columns[0])[columns[1]].to_dict()
253 elif hasattr(data, 'to_dict'):
254 # This is a pd.Series
/usr/local/lib/python3.6/site-packages/pandas/core/frame.py in set_index(self, keys, drop, append, inplace, verify_integrity)
3904 names.append(None)
3905 else:
-> 3906 level = frame[col]._values
3907 names.append(col)
3908 if drop:
/usr/local/lib/python3.6/site-packages/pandas/core/frame.py in __getitem__(self, key)
2683 return self._getitem_multilevel(key)
2684 else:
-> 2685 return self._getitem_column(key)
2686
2687 def _getitem_column(self, key):
/usr/local/lib/python3.6/site-packages/pandas/core/frame.py in _getitem_column(self, key)
2690 # get column
2691 if self.columns.is_unique:
-> 2692 return self._get_item_cache(key)
2693
2694 # duplicate columns & possible reduce dimensionality
/usr/local/lib/python3.6/site-packages/pandas/core/generic.py in _get_item_cache(self, item)
2484 res = cache.get(item)
2485 if res is None:
-> 2486 values = self._data.get(item)
2487 res = self._box_item_values(item, values)
2488 cache[item] = res
/usr/local/lib/python3.6/site-packages/pandas/core/internals.py in get(self, item, fastpath)
4113
4114 if not isna(item):
-> 4115 loc = self.items.get_loc(item)
4116 else:
4117 indexer = np.arange(len(self.items))[isna(self.items)]
/usr/local/lib/python3.6/site-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
3063 return self._engine.get_loc(key)
3064 except KeyError:
-> 3065 return self._engine.get_loc(self._maybe_cast_indexer(key))
3066
3067 indexer = self.get_indexer([key], method=method, tolerance=tolerance)
pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()
pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()
pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()
pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()
KeyError: 'City'