简体   繁体   English

使用 pandas dataframe 中的城市名称向叶 map 添加多个标记

[英]Adding multiple markers to a folium map using city names from pandas dataframe

Im trying to visualize data using folium maps, and I have to plot all Finlands' city names to the map.我试图使用叶图可视化数据,我必须将 plot 将所有芬兰城市名称转换为 map。 I've tried to use pandas dataframe since all my data is in csv format.我尝试使用 pandas dataframe 因为我所有的数据都是 csv 格式。 Here's the code I've tried so far.这是我到目前为止尝试过的代码。

import folium
from folium import plugins
import ipywidgets
import geocoder
import geopy
import numpy as np
import pandas as pd
from vega_datasets import data as vds

m = folium.Map(location=[65,26], zoom_start=5)

# map
map_layer_control = folium.Map(location=[65, 26], zoom_start=5)

# add tiles to map
folium.raster_layers.TileLayer('Open Street Map').add_to(map_layer_control)
folium.raster_layers.TileLayer('Stamen Terrain').add_to(map_layer_control)
folium.raster_layers.TileLayer('Stamen Toner').add_to(map_layer_control)
folium.raster_layers.TileLayer('Stamen Watercolor').add_to(map_layer_control)
folium.raster_layers.TileLayer('CartoDB Positron').add_to(map_layer_control)
folium.raster_layers.TileLayer('CartoDB Dark_Matter').add_to(map_layer_control)

# add layer control to show different maps
folium.LayerControl().add_to(map_layer_control)

# display map
map_layer_control
list = {'REGION': ['Kajaani','Lappeenranta','Pudasjärvi'],
       'CUSTOMERS':['7','4','64']}

list = pd.DataFrame(list)

# geocode address and place marker on map

# map
map_zoo = folium.Map(location=[65,26], zoom_start=4)

# get location information for address
for i in range(0,len(list)):
    address = geocoder.osm(list['REGION'])

# address latitude and longitude
address_latlng = [address.lat, address.lng]

# add marker to map
folium.Marker(address_latlng, popup='INFO', tooltip='Click for more information!').add_to(map_zoo)

# display map
map_zoo

However this code only adds a marker to the last city 'Pudasjärvi'但是,此代码仅在最后一个城市“Pudasjärvi”上添加了一个标记

You can use geopy to get coordinates and then use a loop to add markers to your map:您可以使用geopy获取坐标,然后使用循环将标记添加到 map:

import folium
from geopy.geocoders import Nominatim
import pandas as pd

geolocator = Nominatim(user_agent="example")

l = {'REGION': ['Kajaani','Lappeenranta','Pudasjärvi'],
     'CUSTOMERS':['7','4','64']}

l['COORDS'] = []
for k in l['REGION']:
    loc = geolocator.geocode(k).raw
    l['COORDS'].append((loc['lat'], loc['lon']))

df = pd.DataFrame(l)

map_zoo = folium.Map(location=[65,26], zoom_start=4)

for i,r in df.iterrows():
    folium.Marker(location=r['COORDS'],
                  popup = r['REGION'],
                  tooltip='Click for more information!').add_to(map_zoo)
map_zoo

and you get:你得到:

在此处输入图像描述

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

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