python - 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.
解决方案
推荐阅读
- html - 删除结果项之间的空格
- r - 自动映射中的 autofitVariogram:截止选项不起作用
- c - cJSON 解析在释放 json 对象后给出垃圾值
- linux - 使用 bazel 构建 tensorflow,gcc: internal compiler error: Killed (program cc1plus)
- asp.net-mvc - 在 ASP.NET Core 的 @Html.Dropdownlist 中禁用选定值
- php - 如何确定正在包含的脚本中是否存在函数
- reactjs - 提取特定 React 子元素的正确方法?
- powershell - 将退出代码从 Powershell 传递到 Linux Bash Shell 脚本
- html - 无法在 Firefox 中设置 img 标签的内容属性
- java - 关于 Dijkstra 算法时间复杂度的问题