Search code examples
pandasjupyter-notebookgoogle-maps-markersfolium

How to add the data of this table in markers (pop-up) on map?


I imported this table from Excel into Jupyter. I was using pandas for this. But now I want to mark markers with cities from table below on my map with popups with the data from colognes A1,A2,A3 and I don't know how to do this.

Example: I press on a definite marker and after this the popup appears with this data from colognes A1,A2,A3.

Image 1:

Image 1

Image 2:

Image 2

Can you tell me the corresponding code for this operation?


Solution

  • To display the pop-up in tabular form at each location, a loop process is performed on each row of the data frame, converting one row from a series to a data frame, and then transposing it. I also adjust the width of the data frame.

    import pandas as pd
    import io
    import folium
    
    data = '''
    Name A1 A2 A3 LAT LON
    "Malibu Beach" 0.63 0.55 0.95 34.03194 -118.698387
    Commerce 0.17 0.45 0.25 34.00031 -118.159770
    "Long Beach" 0.19 0.21 0.09 33.77171 -118.181310
    '''
    df = pd.read_csv(io.StringIO(data), delim_whitespace=True)
    
    import folium
    
    m = folium.Map([df.LAT.mean(), df.LON.mean()], zoom_start=8)
    
    for i in range(len(df)):
        html = df.loc[i,['Name','A1','A2','A3']].to_frame().T.to_html(
            classes="table table-striped table-hover table-condensed table-responsive"
        )
        popup = folium.Popup(html, max_width=500)
        folium.Marker([df.iloc[i]['LAT'], df.iloc[i]['LON']], popup=popup).add_to(m)
    
    m
    

    enter image description here