如何使用 Dataframe 在 Folium 上创建集群?
How to make cluster on Folium with Dataframe?
我试图在 Folium 地图上创建一个集群。我有一个数据框如下。
在Folium地图上制作Cluster的代码如下
import os
import folium
import webbrowser
import shutil
import pandas as pd
from folium import plugins
from collections import defaultdict
from pathlib import Path
from folium.plugins import MarkerCluster
my_dir_path = "C:\Users\xxx\Sub_Folder"
results = defaultdict(list)
for file in Path(my_dir_path).iterdir():
with open(file, "r") as file_open:
results["Latitude"].append(file.name[0:9])
results["Longitude"].append(file.name[10:19])
results["Date"].append(file.name[23:25])
results["Month"].append(file.name[20:22])
results["Year"].append(file.name[26:30])
results["Time"].append(file.name[31:36])
results["Type"].append(file_open.read(1))
df_full = pd.DataFrame(results)
#Delete duplicate data
df = df_full.drop_duplicates()
df['Type'] = df['Type'].replace(
to_replace=['0', '1', '2', '3'],
value=['Buffalo', 'Elephant', 'Rhino', 'Zebra'])
df
m = folium.Map(location=[15.170121, 99.159373], zoom_start=11)
folium.LayerControl().add_to(m)
marker_cluster = MarkerCluster().add_to(m)
for row in df.itertuples():
folium.Marker(location=[row.Latitude,row.Longitude],popup=row.Type).add_to(marker_cluster)
m
我运行代码但是地图没有显示任何东西。如何纠正?
可能是先设置了集群,后设置了marker。所以集群设置是在循环过程之后完成的。
import folium
from folium.plugins import MarkerCluster
m = folium.Map(location=[15.170121, 99.159373], zoom_start=12)
marker_cluster = MarkerCluster(
name="clustered name",
).add_to(m)
for row in df.itertuples():
#print(row)
folium.Marker(location=[row.Latitude,row.Longitude],popup=row.Type).add_to(marker_cluster)
folium.LayerControl().add_to(m)
m
我试图在 Folium 地图上创建一个集群。我有一个数据框如下。
在Folium地图上制作Cluster的代码如下
import os
import folium
import webbrowser
import shutil
import pandas as pd
from folium import plugins
from collections import defaultdict
from pathlib import Path
from folium.plugins import MarkerCluster
my_dir_path = "C:\Users\xxx\Sub_Folder"
results = defaultdict(list)
for file in Path(my_dir_path).iterdir():
with open(file, "r") as file_open:
results["Latitude"].append(file.name[0:9])
results["Longitude"].append(file.name[10:19])
results["Date"].append(file.name[23:25])
results["Month"].append(file.name[20:22])
results["Year"].append(file.name[26:30])
results["Time"].append(file.name[31:36])
results["Type"].append(file_open.read(1))
df_full = pd.DataFrame(results)
#Delete duplicate data
df = df_full.drop_duplicates()
df['Type'] = df['Type'].replace(
to_replace=['0', '1', '2', '3'],
value=['Buffalo', 'Elephant', 'Rhino', 'Zebra'])
df
m = folium.Map(location=[15.170121, 99.159373], zoom_start=11)
folium.LayerControl().add_to(m)
marker_cluster = MarkerCluster().add_to(m)
for row in df.itertuples():
folium.Marker(location=[row.Latitude,row.Longitude],popup=row.Type).add_to(marker_cluster)
m
我运行代码但是地图没有显示任何东西。如何纠正?
可能是先设置了集群,后设置了marker。所以集群设置是在循环过程之后完成的。
import folium
from folium.plugins import MarkerCluster
m = folium.Map(location=[15.170121, 99.159373], zoom_start=12)
marker_cluster = MarkerCluster(
name="clustered name",
).add_to(m)
for row in df.itertuples():
#print(row)
folium.Marker(location=[row.Latitude,row.Longitude],popup=row.Type).add_to(marker_cluster)
folium.LayerControl().add_to(m)
m