Search code examples
pandaspython-3.5folium

How to unpack pair of indexes of list items


I have got a csv file which looks like the following:

VOLCANX020,NUMBER,NAME,LOCATION,STATUS,ELEV,TYPE,TIMEFRAME,LAT,LON
509.000000000000000,1201-01=,Baker,US-Washington,Historical,3285.000000000000000,Stratovolcanoes,D3,48.7767982,-121.8109970
511.000000000000000,1201-02-,Glacier Peak,US-Washington,Tephrochronology,3213.000000000000000,Stratovolcano,D4,48.1118011,-121.1110001
513.000000000000000,1201-03-,Rainier,US-Washington,Dendrochronology,4392.000000000000000,Stratovolcano,D3,46.8698006,-121.7509995
515.000000000000000,1201-05-,St. Helens,US-Washington,Historical,2549.000000000000000,Stratovolcano,D1,46.1997986,-122.1809998

and i want to use the pair of LAT and LON Columns to pass them as argument for creating a map using folium library.

I was able to extract the csv file into a variable using pandas library:

data = pandas.read_csv('Volcanoes_USA.txt')

and create a basic map using folium using:

mymap = folium.Map(location=[12.96697, 77.58728], zoom_start=6)

Now using the csv file data which is loaded in data variable, i want to use all these coordinates (pair of LAT and LON) values to update the map. One way is to extract the LAT and LON in separate variables.

lat = list(data["LAT"])
lon = list(data["LON"])

Is there a way where i can unpack the list items in such a way that i could use a single variable coor which holds the pair of LAT and LON so that i could use it in the for loop in the following way.

for coordinates in coor:
  fg.add_child(folium.Marker(location=coor, popup="This is a Marker", icon=folium.Icon(color='green')))

Solution

  • You can use zip. Like so:

    map_1 = folium.Map(location=[45.372, -121.6972],
                       zoom_start=11,
                       tiles='Stamen Terrain')
    
    
    lat = [45.3288,45.25,45.3311,]
    lon = [-121.6625,-121.7113,-121.75]
    
    for cords in zip(lat, lon):
        folium.Marker(cords).add_to(map_1)
    
    map_1
    

    resulting in this map:

    enter image description here