对两个熊猫数据框使用带有geopy.distance.vincenty的.apply函数

时间:2018-09-30 16:54:56

标签: python python-3.x pandas

目标是找到距离小于15米的所有坐标。数据从两个数据帧中存储。

计算距离的代码如下:

def dist_TwoPoints_LatLong(Lat_1,Long_1,Lat_2,Long_2):
    coords_1 = (Lat_1,Long_1)
    coords_2 = (Lat_2,Long_2)

    #print ("Distance in km :",geopy.distance.vincenty(coords_1, coords_2).km)
    if  (geopy.distance.vincenty(coords_1, coords_2).m <15):
        #print ("Distance in m: ",geopy.distance.vincenty(coords_1, coords_2).m)
        return geopy.distance.vincenty(coords_1, coords_2).m
    else: 
        return 10000

对于每个坐标:

    for indexR, rowR in R.iterrows():
         range_15m = M.apply(lambda row: dist_TwoPoints_LatLong(rowR['lat_t'],rowR['lon_t'],row['lat'],row['long']), axis=1)

R中的数据框:

lat_t   lon_t   y   speed_t sprung_weight_t duration_capture    EventDate   EventTime
-27.7816    22.9939 4   27.1    442.0   2.819999933242798   2017/11/01  12:09:15
-27.7814    22.9939 3   27.3    447.6   2.8359999656677246  2017/11/01  12:09:18
-27.7812    22.9939 3   25.4    412.2   2.884000062942505   2017/11/01  12:09:21
-27.7809    22.994  3   26.1    413.6   2.9670000076293945  2017/11/01  12:09:23
-27.7807    22.9941 3   25.4    395.0   2.938999891281128   2017/11/01  12:09:26
-27.7805    22.9941 3   21.7    451.9   3.2829999923706055  2017/11/01  12:09:29
-27.7803    22.9942 3   20.2    441.7   3.6730000972747803  2017/11/01  12:09:33
-27.7801    22.9942 4   16.7    443.3   4.25                2017/11/01  12:09:36
-27.7798    22.9942 3   15.4    438.2   4.819000005722046   2017/11/01  12:09:41
-27.7796    22.9942 3   15.4    436.1   5.0309998989105225  2017/11/01  12:09:45
-27.7794    22.9942 4   15.8    451.6   5.232000112533569   2017/11/01  12:09:50
-27.7793    22.9941 3   18.2    439.4   4.513000011444092   2017/11/01  12:09:56
-27.7791    22.9941 3   21.4    413.7   3.8450000286102295  2017/11/01  12:10:00
-27.7788    22.994  3   23.1    430.8   3.485999822616577   2017/11/01  12:10:04

M中的数据框:

lat        lon      EventDate   EventTime
-27.7786    22.9939 2017/11/01  12:10:07
-27.7784    22.9939 2017/11/01  12:10:10
-27.7782    22.9939 2017/11/02  12:10:14
-27.778     22.9938 2017/11/02  12:10:17
-27.7777    22.9938 2017/11/02  12:10:21

如何使用两个具有.apply功能的数据框来改进代码?如果范围小于15,则应将数据帧M的行存储在新的数据帧中。

0 个答案:

没有答案