【问题标题】:How can I calculate the distance between two points based on matching attributes with GeoPandas?如何根据与 GeoPandas 匹配的属性计算两点之间的距离?
【发布时间】:2020-01-03 20:16:46
【问题描述】:

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

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

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().

不知道这是什么意思。

【问题讨论】:

  • 我问了一个非常相似的问题here。然后,我使用组合数据集获取纬度/经度,并使用来自geopy 库的距离函数。也许这会让你走上正确的道路
  • 计数使用这个: len(will_df[will_df['Residence_ID'].isin(pd.unique(res_df['Residence ID'])]) 它会给你will_df中的记录数在 res_df 中具有相同的 Residence_ID。对于您的问题,我建议您将其分为两步,第一步使用上述方法找到公共记录,然后在下一步找到它们之间的距离。
  • @No_body 我在您编写的代码中遇到语法错误,因此不确定使用的正确方法是什么
  • @No_body 您的代码的问题还在于它只返回唯一值——我不需要唯一值,我需要匹配对的输出。
  • @MattR 所以问题是,您的问题/问题不包括我的项目中引发问题的部分,即我需要根据特定标准匹配成对的值(即相同ID 名称),您在问题中不会这样做。

标签: python pandas geopandas


【解决方案1】:

对于您问题的第一部分,

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(或其他内容)。

如果您可以发布一些示例数据,我们可以为您提供帮助,或者您可以搜索 docs/SO。

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

【讨论】:

    猜你喜欢
    • 2010-10-30
    • 1970-01-01
    • 2011-04-23
    • 2014-11-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多