代码之家  ›  专栏  ›  技术社区  ›  plast1cd0nk3y

如何基于与GeoPandas匹配的属性计算两点之间的距离?

  •  0
  • plast1cd0nk3y  · 技术社区  · 6 年前

    我有两个包含点坐标信息的CSV文件。一个文件有11个唯一ID的列表(我们称之为文件1),每个ID对应一个lat/lon坐标。另一个文件(我们称之为文件2)有大约300个lat/lon点,每个点匹配这11个唯一id中的一个。我要做的是检查文件2中的一个点是否具有与文件1中的一个点相同的ID,如果是,请计算这两个点之间的距离。

    我已尝试使用以下代码执行此操作:

    for index, row in res_df:
        for windex, wrow in will_df: 
            if res_df['Residence_ID'] == will_df['Residence_ID']:
                print(res_df.distance(will_df))
    

    但是,当我尝试此操作时,会出现以下错误:

    ValueError                                Traceback (most recent call last)
    <ipython-input-73-b4268cef3e71> in <module>()
    ----> 1 for index, row in res_df:
          2   for windex, wrow in will_df:
          3     if res_df['Residence_ID'] == will_df['Residence_ID']:
          4       print(res_df.distance(will_df))
          5 
    
    ValueError: too many values to unpack (expected 2)
    
    

    我也曾尝试过使用iterrows,但这并没有解决我的问题。

    此外,我想尝试计算匹配记录的数量,我在这里也遇到了一个问题:

    counter = 0
    for id1 in res_df['Residence ID']:
      for id2 in will_df['Residence_ID']:
        if id1 == id2:
          print("match")
          counter += 1
    print(counter)
    
    

    当我运行上面的代码时,我的计数器返回值52;但是,这没有意义,因为我在文件2中的300条记录都与文件1中的一些记录匹配。所以,我想我缺少一些基本的逻辑。

    编辑:

    for index, row in res_df.items():
      for windex, wrow in will_df.items(): 
        if res_df['Residence_ID'] == will_df['Residence_ID']:
          print(res_df.distance(will_df))
    

    错误信息是:

    ValueError                                Traceback (most recent call last)
    <ipython-input-80-65e9f78ad2a5> in <module>()
          1 for index, row in res_df.items():
          2   for windex, wrow in will_df.items():
    ----> 3     if res_df['Residence_ID'] == will_df['Residence_ID']:
          4       print(res_df.distance(will_df))
    
    /usr/local/lib/python3.6/dist-packages/pandas/core/generic.py in __nonzero__(self)
       1553             "The truth value of a {0} is ambiguous. "
       1554             "Use a.empty, a.bool(), a.item(), a.any() or a.all().".format(
    -> 1555                 self.__class__.__name__
       1556             )
       1557         )
    
    ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
    

    不知道这是什么意思。

    0 回复  |  直到 6 年前
        1
  •  0
  •   Evan    6 年前

    for index, row in res_df:
    

    将返回错误,因为 for index, row 需要两个项目。你只给它一个, res_df .

    for index, row in (df.iterrows()):
         print(index, "\n", row, "\n\n")
    

    为了你的第二次尝试,

          1 for index, row in res_df.items():
          2   for windex, wrow in will_df.items():
    ----> 3     if res_df['Residence_ID'] == will_df['Residence_ID']:
    

    这会导致错误,因为您正在比较 整个系列 属于 res_df["Residence_ID"] 具有 will_df['Residence_ID'] . 如果您致力于循环(稍后我将继续讨论,您不应该这样做),您希望使用 row["Residence_ID"] == wrow["Residence_ID"] .

    现在,您似乎正在尝试计算距离(不确定是否有用于该距离的自定义函数,或者它是否是GeoPandas的一部分),但您可能想做的是 join 将这两个数据帧放在一起,然后计算一个新列 distance_between (或某事)基于连接的数据帧。

    如果你可以发布一些样本数据,我们可以帮助你,或者你可以搜索文档/销售订单。

    更多关于连接的信息: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.join.html