从df中提取满足条件的任何2行-Pandas

时间:2019-03-07 07:24:11

标签: python pandas filter difference haversine

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

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

Haversine只是用python内置的简单

示例为

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

感谢您的光临

1 个答案:

答案 0 :(得分:-1)

一般语法为:

@RunWith(SpringRunner.class)
@SpringBootTest
@TestPropertySource(properties = { "spring.config.location=classpath:application.yml" })
public class ServiceTest {...}

在上面添加您自己的条件来替换,您将得到答案,因为您的问题并未清楚说明您的条件是什么

answerdf = df.loc[df[<cond1> & <cond2> & <cond3>]] 的示例:

<cond>

放在上面时:

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