如何遍历数据框以创建单独的Folium贴图?

时间:2020-01-25 11:11:32

标签: python python-3.x loops dataframe folium

我正在尝试使用foursqaure检查5个不同城市中的寿司店。 我可以获取数据并正确过滤。下面的代码。

city = {'City':['Brunswick','Auckland','Wellington','Christchurch','Hamilton','Ponsonby'],
    'Latitude':[-37.7670,-36.848461,-41.28664,-43.55533,-37.78333,-36.8488],
    'Longitude':[144.9621,174.763336,174.77557,172.63333,175.28333,174.7381]}
df_location= pd.DataFrame(city, columns = ['City','Latitude','Longitude'])

def getNearbyVenues(names, latitudes, longitudes, radius=2000, LIMIT=100):
venues_list=[]
for name, lat, lng in zip(names, latitudes, longitudes):
    # create the API request URL
    url = 'https://api.foursquare.com/v2/venues/explore?&client_id={}&client_secret={}&v={}&ll={},{}&radius={}&limit={}&categoryId={}'.format(
        CLIENT_ID, 
        CLIENT_SECRET, 
        VERSION,
        lat,
        lng,
        radius,
        LIMIT,
        "4bf58dd8d48988d1d2941735")
    # make the GET request
    results = requests.get(url).json()["response"]['groups'][0]['items']
    venues_list.append([(
        name, 
        v['venue']['name'], 
        v['venue']['location']['lat'], 
        v['venue']['location']['lng']) for v in results])
nearby_venues = pd.DataFrame([item for venue_list in venues_list for item in venue_list])
nearby_venues.columns = [
              'City',
              'Venue', 
              'Venue Latitude', 
              'Venue Longitude',]
return(nearby_venues)

sushi_venues = getNearbyVenues(names = df_location['City'],
                               latitudes = df_location['Latitude'],
                               longitudes = df_location['Longitude'])
cities = df_location["City"]
latitude = df_location["Latitude"]
longitude = df_location["Longitude"]

我被困在创建地图上,我不确定如何遍历城市为每个城市创建地图。 这是我的代码。

maps = {}
for city in cities:
maps[city] = folium.Map(location = [latitude, longitude],zoom_start=10)


for lat, lng, neighborhood in zip(sushi_venues['Venue Latitude'], sushi_venues['Venue Longitude'], sushi_venues['Venue']):
    label = '{}'.format(neighborhood)
    label = folium.Popup(label, parse_html = True)
    folium.CircleMarker(
    [lat, lng],
    radius = 5,
    popup = label,
    color = 'blue',
    fill = True,
    fill_color = '#3186cc',
    fill_opacity = 0.7,
    parse_html = False).add_to(maps[city])

maps[cities[0]]

对于此代码,“ maps [cities [0]]”显示空白的作品集地图。

如果我更改代码以引用df_location中的城市行,例如

maps = {}
   for city in cities:
   maps[city] = folium.Map(location = [latitude[0], longitude[0],zoom_start=10)

然后,“ maps [cities [0]]”将显示正确标记的不伦瑞克省Folio地图,并标出相应的地点。 所以我的问题是,如何正确遍历所有5个城市,以便我可以为每个城市绘制一张新地图而无需每次更改位置?我无法压缩位置,因为初始化Folium地图需要一个纬度/经度。

非常感谢您的帮助!

0 个答案:

没有答案