11

I am trying to plot a large number (~20,000) of circle markers using Folium. The latitude and longitude data is contained in a Pandas DataFrame (within "LAT" and "LONG" columns). I have come up with the following (inefficient) code, which requires iterating through the dataframe row by row. Not surprisingly, it takes quite a while to plot the map. Is there a better/faster way to accomplish this?

Meanwhile, I do not have to use Folium. If there is a more suitable tool that you know of (I still have to keep the data in a Pandas DataFrame though), please let me know.

Thanks!

map_osm = folium.Map(location=[43.094768, -75.348634])
for index, row in df.iterrows():
    folium.CircleMarker(location=[row["LAT"], row["LONG"]]).add_to(map_osm)
map_osm
marillion
  • 10,618
  • 19
  • 48
  • 63

2 Answers2

13

Use apply along the column axis:

df.apply(lambda row:folium.CircleMarker(location=[row["LAT"], 
                                                  row["LONG"]]).add_to(map_osm),
         axis=1)
Zeugma
  • 31,231
  • 9
  • 69
  • 81
  • I tried it, but it does not seem to speed the process up. Using the `%%timeit` magic, I tested the first 5 records and ended up with around 200 ms, for both my code and yours. – marillion Oct 31 '16 at 03:50
  • Can you add a list of circles at once to the map or are you forced by folium to add circles one by one ? – Zeugma Oct 31 '16 at 04:28
  • 1
    It looks like Folium does not support vectorized data entry. At least, all examples I found needed row by row iteration. I may need to find another solution it seems. – marillion Oct 31 '16 at 15:50
1

use this example, I hope this help!

#Create the Map
map_osm = folium.Map(
    location = [43.094768, -75.348634],
    zoom_start = 6
)
map_osm
#You Markler the point in Map
for indice, row in df.iterrows():
    folium.Marker(
        location=[row["LAT"], row["LONG"]],
        popup=row['NAME_COLUM'],
        icon=folium.map.Icon(color='yellow')
    ).add_to(map_osm)
map_osm