如何使用pandas和python来评估用户所在的地理位置?

2024-04-28 13:47:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我正试图弄清楚Uber和UbersEats等应用程序是如何运作和管理物流的。我想知道我们如何构建一个函数来计算用户位于多少个框内。 这是用户的数据:

user_id,loc_lat,loc_lon
1,55.737564,37.345186
2,56.234564,37.234590
3,55.234578,36.295745

这是方框(位置)的坐标:

place_id,loc_lat,loc_lon,point_number
1,55.747022,37.787073,0
1,55.751713,37.784328,1
1,55.753878,37.777638,2
1,55.751031,37.779351,3
2,55.803885,37.458311,0
2,55.808677,37.464054,1
2,55.809763,37.461314,2
2,55.810840,37.458654,3

因此,对于用户1,它将是2个可用位置,而对于用户3,它将是0。 如果你能给我指出正确的方向,我将不胜感激


Tags: 数据函数用户id应用程序loclonlat
1条回答
网友
1楼 · 发布于 2024-04-28 13:47:22

从用户的测试用例来看:没有一个在框的范围内。遵循 How can I determine whether a 2D Point is within a Polygon?

// p is your point, p.x is the x coord, p.y is the y coord
if (p.x < Xmin || p.x > Xmax || p.y < Ymin || p.y > Ymax) {
    // Definitely not within the polygon!
}

在Python中加载数据帧:

import pandas as pd
columns = ['user_id','loc_lat','loc_lon','point_number']
df = pd.DataFrame([[1,55.747022,37.787073,0],[1,55.751713,37.784328,1],[1,55.753878,37.777638,2],[1,55.751031,37.779351,3]], columns = columns)

相关问题 更多 >