循环遍历pandas数据帧以创建单独的Folium映射



我有一个pandas数据帧,看起来像这样:

import pandas as pd
d = {'user': ['Peter', 'Peter', 'Peter', 'Peter', 'David', 'David', 'David', 'Emma', 'Joyce', 'Joyce', 'Joyce'], 'date': ['2019-03-04', '2019-03-04', '2019-03-04', '2019-03-04', '2019-03-04', '2019-03-04', '2019-03-04', '2019-03-04', '2019-03-04', '2019-03-04', '2019-03-04'], 'lat': [37.749798119650926, 37.751028173710736, 37.751698332490214, 37.75180012822952, 38.122893081890844, 38.124108467926035, 38.12743379574882, 37.89363489791644, 37.53620628385582, 37.53804390907164, 37.54044296272588], 'lon': [-122.49230146408082, -122.49229073524474, -122.49170064926147, -122.48974800109862, -122.24205136299133, -122.23907947540283, -122.23867177963257, -122.07653760910033, -121.99707984924316, -121.99315309524536, -121.9914150238037]}
df = pd.DataFrame(data=d)
df
user    date        lat         lon
Peter   2019-03-04  37.749798   -122.492301
Peter   2019-03-04  37.751028   -122.492291
Peter   2019-03-04  37.751698   -122.491701
Peter   2019-03-04  37.751800   -122.489748
David   2019-03-04  38.122893   -122.242051
David   2019-03-04  38.124108   -122.239079
David   2019-03-04  38.127434   -122.238672
Emma    2019-03-04  37.893635   -122.076538
Joyce   2019-03-04  37.536206   -121.997080
Joyce   2019-03-04  37.538044   -121.993153
Joyce   2019-03-04  37.540443   -121.991415

我想为每个用户创建四个单独的folium地图,将经纬度坐标显示为地图上的点以及这些点之间的线。最后,我想得到四个以用户命名的映射:Peter.htmlDavid.htmlEmma.htmlJoyce.html

到目前为止,我已经提出了以下内容,但这只会生成绘制了所有坐标的文件Peter.html。你知道我错过了什么吗?

import folium
# Create map
m = folium.Map(location=[37.733795, -122.446747], 
zoom_start=9, 
min_zoom=10, 
max_zoom=19,
control_scale=True)
# Add location markers for GPS points to holding object
for user, data in df.groupby('user'):
for row in df.itertuples():
folium.CircleMarker(
location=[row.lat, row.lon],
radius=4,
fill=True,
fill_opacity=0.5
).add_to(m)
# Add blue polyline between raw GPS measurements
folium.PolyLine(
locations=df[['lat','lon']].values,
opacity=0.8,
weight=3
).add_to(m)
m.save(df['user'].iloc[0]+".html")

按用户过滤主数据帧,并根据过滤后的数据帧创建映射。

import pandas as pd
import folium
d = {}
df = pd.DataFrame(data=d)
users = list(df.user.unique())
def create_user_map(user):
m = folium.Map(location=[37.733795, -122.446747], 
zoom_start=9, 
min_zoom=10, 
max_zoom=19,
control_scale=True)

df_user = df[df.user==user]
for row in df_user.itertuples():
folium.CircleMarker( location=[row.lat, row.lon],
radius=4,
fill=True,
fill_opacity=0.5).add_to(m)
# Add blue polyline between raw GPS measurements
folium.PolyLine(
locations=df_user[['lat','lon']].values,
opacity=0.8,
weight=3).add_to(m)
return m
for user in users:
user_map = create_user_map(user)
user_file = f"{user}.html"
user_map.save(user_file)

相关内容

  • 没有找到相关文章