[英]How do you create locations markers from a pandas dataframe to a folium map
I am trying to take a pandas dataframe that has the longitude and latitude for the zip codes (US postal code system) with the highest per capita income.我正在尝试使用具有人均收入最高的邮政编码(美国邮政编码系统)的经度和纬度的熊猫数据框。
The dataframe I'm working from has 7 columns (zip , lat, lng, rank, designation, population, per capital income).我正在使用的数据框有 7 列(zip、lat、lng、rank、名称、人口、人均收入)。
I then create a base map to use from folium centered roughly on the US.然后,我创建了一个底图,以使用大致以美国为中心的大叶草。
lati = 37.09024
longi = -95.712891
map_usa = folium.Map(location=[lati, longi], zoom_start = 10)
map_usa
This map outputs fine.该地图输出良好。
I then am attempting to run a loop to go through the data frame to plot the individual rows, but run into an error.然后我试图运行一个循环来遍历数据框以绘制各个行,但遇到错误。 Code and error message is below.
代码和错误信息如下。
for i in df_zip_top_income.iterrows():
lat = df_zip_top_income.at[i, 'lat']
lng = df_zip_top_income.at[i, 'lng']
town = df_zip_top_income.at[i, 'designation']
folium.Marker (location=[lat,lng], popup = town, icon = folium.Icon(color='blue')).add_to(map_usa)
> > --------------------------------------------------------------------------- ValueError Traceback (most recent call
> last) <ipython-input-59-9099f5db86dd> in <module>
> 1 for i in df_zip_top_income.iterrows():
> ----> 2 lat = df_zip_top_income.at[i, 'lat']
> 3 lng = df_zip_top_income.at[i, 'lng']
> 4 town = df_zip_top_income.at[i, 'designation']
> 5
>
> /opt/conda/envs/Python-3.7-main/lib/python3.7/site-packages/pandas/core/indexing.py
> in __getitem__(self, key) 2176 raise
> ValueError("Invalid call for scalar access (getting)!") 2177
> -> 2178 key = self._convert_key(key) 2179 return self.obj._get_value(*key, takeable=self._takeable) 2180
>
> /opt/conda/envs/Python-3.7-main/lib/python3.7/site-packages/pandas/core/indexing.py
> in _convert_key(self, key, is_setter) 2212 if not
> is_integer(i): 2213 raise ValueError(
> -> 2214 "At based indexing on an integer index " 2215 "can only have integer indexers"
> 2216 )
>
> ValueError: At based indexing on an integer index can only have
> integer indexers
It seems to be an issue with dataypes, but I'm not sure at this point how to most effectively resolve.这似乎是数据类型的问题,但我目前不确定如何最有效地解决。
df_zip_top_income.dtypes
zip int64
lat float64
lng float64
rank float64
designation object
population float64
per_cap_inc float64
dtype: object
Thanks in advance, and please let me know if further information is needed.提前致谢,如果需要更多信息,请告诉我。
iterrows
returns a tuple with an index on the first position and a Series on the second. iterrows
返回一个元组,第一个位置有一个索引,第二个位置有一个系列。 You cannot use it to index your dataframe.你不能用它来索引你的数据框。 Instead do:
而是这样做:
for index, series in df_zip_top_income.iterrows():
lat = series['lat']
lng = series['lng']
town = series['designation']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.