从满足条件的df中提取任意2行

2024-04-18 08:54:15 发布

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

我尝试使用df(下面7行采样)来提取满足3个条件的任意两行:

  1. 第一个位置之间的距离小于x英里(使用哈弗森)
  2. 第二个位置之间的距离小于x英里(使用哈弗森)
  3. 创建时间之间的差异小于x分钟

Haversine只不过是用python简单地构建的

示例as

from haversine import haversine

lyon = (45.7597, 4.8422) # (lat, lon)
paris = (48.8567, 2.3508)

haversine(lyon, paris, unit='mi')
243.71201856934454  # in miles

当我说第一个位置之间的差异时,这只是第一个位置之间的差异,不考虑第二个位置或第二个坐标(因此任何两个第一个位置之间的x英里差异和任何两个第二个位置之间的x英里差异)

       DAY     Order  1st_latitude  1st_longitude 2nd_latitude 2nd_longitude    created_time
        1/3/19  234e    32.69        -117.1          32.63      -117.08   3/1/19 19:00
        1/3/19  235d    40.73        -73.98          40.73       -73.99   3/1/19 23:21
        1/3/19  253w    40.76        -73.99          40.76       -73.99   3/1/19 15:26
        2/3/19  231y    36.08        -94.2           36.07       -94.21   3/2/19 0:14
        3/3/19  305g    36.01        -78.92          36.01       -78.95   3/2/19 0:09
        3/3/19  328s    36.76        -119.83         36.74       -119.79  3/2/19 4:33
        3/3/19  286n    35.76        -78.78          35.78       -78.74   3/2/19 0:43

谢谢你找我


Tags: fromimport距离示例dfas时间差异
1条回答
网友
1楼 · 发布于 2024-04-18 08:54:15

一般语法为:

answerdf = df.loc[df[<cond1> & <cond2> & <cond3>]]

在上面提出你自己的条件来替换,你会得到你的答案,因为你的问题没有提供一个清楚的解释你的条件是什么

<cond>示例:

haversine((df['1st_latitude'], df['1st_longitude']),(df['2nd_latitude'], df['2nd_longitude']) , unit='mi') > 100

当置于上述位置时:

answerdf = df.loc[df[haversine((df['1st_latitude'], df['1st_longitude']),(df['2nd_latitude'], df['2nd_longitude']) , unit='mi') > 100]]

相关问题 更多 >