首页 > 解决方案 > How to draw lines using Polyline and use coordinates from a pandas dataframe

问题描述

I am trying to plot a map using folium and I was able to mark the coordinates with folium.Marker. Now I want to draw lines between each set of coordinates. I tried with folium.PolyLine with lamda and also with for loop. However, I am getting errors in both methods.

This is a sample dataframe :I want to draw a line for each row connecting Pickup coordinates to delivery coordinates.

Order_Nu   Pickup_Lat   Pickup_lng  Del_Lat      Del_lng    

0 3491865 41.95586 -79.98764 40.39827 -75.94362
1 3478258 40.04115 -75.48032 41.09181 -75.34138
2 3469212 40.18448 -79.07961 40.11679 -75.01276

Method 01:

df_filtered_all.apply(lambda row:folium.PolyLine(locations=[ [row['Pickup_Lat'], 
                                                     row['Pickup_lng']]
                                                     , [row['Del_Lat']
                                                     , row['Del_lng']] ]
                                         , color='red') 
                                                                                
                                        ) .add_to(m)

Error:

--> 354 raise KeyError(key) 355 return super().get_loc(key, method=method, tolerance=tolerance) 356

KeyError: 'Pickup_Lat'

Method 02:

for row in df_filtered_all.itertuples(index = True, name ='Pandas'):

       folium.PolyLine([[getattr(row, "Pickup_Lat"), getattr(row, "Pickup_lng")] ,
                 [getattr(row, "Del_Lat"), getattr(row, "Del_lng")] ]  ).add_to(m)

Error: RecursionError: maximum recursion depth exceeded while calling a Python object

Any help would be highly appreciated.

标签: pythonpandaspolylinefolium

解决方案


推荐阅读