Pandas 如何将两列数据传递到使用folium制作的choropleth地图标记的弹出标签?
我希望每个弹出窗口都有条形图的名称和在WB100列表上的位置,列:条形图,wb_pos。这是我的代码,因为每个标记的弹出窗口都是相同的=“Tokyo”,请参见所附的数据框和输出 请参见以下部分数据框:Pandas 如何将两列数据传递到使用folium制作的choropleth地图标记的弹出标签?,pandas,markers,folium,Pandas,Markers,Folium,我希望每个弹出窗口都有条形图的名称和在WB100列表上的位置,列:条形图,wb_pos。这是我的代码,因为每个标记的弹出窗口都是相同的=“Tokyo”,请参见所附的数据框和输出 请参见以下部分数据框: data={'Bar':{0:'Connaught Bar',1:'Dante',2:'theclumneses',3:'Atlas',4:'Tayēr+Elementary',City':{0:'London',1:'Athens',3:'Singapore',4:'UK Delivery':{
data={'Bar':{0:'Connaught Bar',1:'Dante',2:'theclumneses',3:'Atlas',4:'Tayēr+Elementary',City':{0:'London',1:'Athens',3:'Singapore',4:'UK Delivery':{0:0,1:1,2:1,3:1,4:1},'Latitude':3:1.3521,4:23.7275},'经度:{0:0:0:0.1278,1:74.0060,2:0:0 0.1278,1:74.75,1:51.507351,1:51.507351,1:51.507351,1:51.50.7351,1:51.7351,1:50.7351,1:50.7351,1:50.7351,1:51,1:50.7351,1:51,1:51.7351,1:51,1:50.7351,1:51,1:50.7351,1:75,1:51,1:40.7351,1:40.7351,1:51,1:40.717,1:50,1:40.7151,1:40.7351,1:40.7351,1.7151,1:50,1:40.7151,1:50,1.71},第一次提交:{0:0,1:0,2:0,3:0,4:0},'wb_pos':{0:1,1:2,2:3,3:4,4:5}
dfcv=pd.DataFrame(数据)
将熊猫作为pd导入
进口叶
进口布兰卡
从folium.plugins导入MarkerCluster
world_geo=r'world_countries.json'#geojson文件
世界地图=folium.map(位置=[0,0],缩放开始=3,宽度=1100,高度=800,瓷砖='cartodb positon',最大边界=真)
世界地图(
name='choropleth WB100',
地理位置数据=世界地理位置,
数据=dfcv,
列=[“国家”,“总数”],
key_on='feature.properties.name',
nan_fill_color='white',
nan_填充_不透明度=0.1,
填充颜色为蓝色,
填充不透明度=0.7,
线条不透明度=0.2,
legend_name='World Best 100酒吧'
)
世界地图
mapa=folium.map.FeatureGroup()
#循环遍历WB100并添加每个条形图位置、名称和WB100位置
用于拉特、液化天然气、酒吧、邮政编码位置(测向纬度、,
经度,
df.Bar,
df.wb_位置):
mapa.add_子对象(
圆叶(
[拉丁美洲,液化天然气],
标签='{},{}'。格式(条形图,位置),
弹出=标签,
半径=5,#定义圆标记的大小
颜色='黄色',
填充=真,
填充颜色为蓝色,
填充不透明度=0.6
).将_添加到(mapa)
)
世界地图。添加子地图(mapa)
您的代码中有一些错误,下面是一个基于输入数据的“wb_pos”字段的工作示例,因为您实际上没有任何
总计
字段
import pandas as pd
df = pd.DataFrame(df) # Needed to convert your dict to an actual DataFrame
world_geo = 'https://raw.githubusercontent.com/jdamiani27/Data-Visualization-and-D3/master/lesson4/world_countries.json' # geojson file
world_map = folium.Map(
location = [0, 0],
zoom_start = 3,
width = 1100,
height = 800,
tiles = 'cartodb positron',
max_bounds = True
)
world_map.choropleth(
name = 'choropleth WB100',
geo_data = world_geo,
data = df, # Replaced dfcv because it was not defined in your input data
columns = ['Country', 'wb_pos'], # Replaced the 'Total' by another random column, here 'wb_pos'
key_on = 'feature.properties.name',
nan_fill_color = 'white',
nan_fill_opacity = 0.1,
fill_color = 'Blues',
fill_opacity = 0.7,
line_opacity = 0.2,
legend_name = 'Worlds Best 100 Bars'
)
world_map
mapa = folium.map.FeatureGroup()
# loop through the WB100 and add each Bar location, name and WB100 position
for lat, lng, bar, pos in zip(
df.Latitude,
df.Longitude,
df.Bar,
df.wb_pos):
label= '{}, {}'.format(bar, pos) # Define label here to reuse after
mapa.add_child(
folium.CircleMarker(
[lat,lng],
label = label,
popup = label,
radius = 5, # define how big you want the circle markers to be
color = 'yellow',
fill = True,
fill_color = 'blue',
fill_opacity = 0.6
).add_to(mapa)
)
# Build a CircleMarker for each row based on the lat long of the record
df.apply(lambda row: folium.CircleMarker(
location=[row["Latitude"],
row["Longitude"]],
radius=10,).add_to(world_map), axis=1)
world_map.add_child(mapa)
我还必须修复您的标签
,该标签引发了一个错误,因为当参数传递给函数时,您不能重用同一参数列表中的参数,您需要在之前定义它
下面是相应的结果:
当“总计”字段可用时,数字将固定
另外,请注意,对于所有具有地理性质的问题,都存在着。
NameError:name'dfcv'没有定义
->什么是dfcv?另外,您的数据中没有Total
字段。非常感谢!@s.k这是我的第一个项目,这里显然犯了不少错误,再次感谢您的帮助。