首页 > 解决方案 > HTML/CSS 弹出显示列表中的文本

问题描述

是否可以在弹出方法中使用熊猫数据框(或列表)中的特定项目?

例如,而不是写:

popup = '<strong>Item</strong>'

编写自动执行此程序而不是编写 100 个弹出窗口?

popup = df.iloc[i,1]'

for i in range(100):
    folium.Marker([df.iloc[i,2], df.iloc[i,3]], popup = '<strong> Location</strong>', 
                  tooltip = tooltip, icon = folium.Icon(icon = 'cloud')).add_to(m)

标签: pythonhtmlcsspopupfolium

解决方案


是的你可以。无需过多修改代码

for i in range(100):
    folium.Marker([df.iloc[i,2], df.iloc[i,3]], 
                  popup='<strong> {}</strong>'.format(df.iloc[i,1]), 
                  tooltip = tooltip, icon=folium.Icon(icon='cloud')).add_to(m)

但是考虑一下其他几种流行的方法来在数据框中绘制事物。

首先使用iterrows

for _, row in df.iterrows():
    folium.Marker([row['your_lat_col'], row['your_long_col'], 
                  popup='<strong> {}</strong>'.format(row['your_popup_col']), 
                  tooltip = tooltip, icon=folium.Icon(icon='cloud')).add_to(m)

注意:itertuples没有那么流行,但速度更快且非常相似。但是,在使用 itertuples 时,您的列名不能有空格。

此外,Python 的 zip() 使迭代变得容易

for lat, lon, popup in zip(df['your_lat_col'], df['your_long_col'], df['your_popup_col']):
        folium.Marker([lat, lon, popup='<strong> {}</strong>'.format(popup), 
                      tooltip = tooltip, icon=folium.Icon(icon='cloud')).add_to(m)

推荐阅读