【发布时间】: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 名称),您在问题中不会这样做。