如何在两个数据框上进行double for循环



我有一个由未知位置组成的数据框架,只是一组纬度和经度。这个列表包含了许多几乎有相同坐标的地方。我想创建一个带有"过滤未知位置"的新数据框架,其中几乎相同的位置被合并到一个地方。对于每一个"被过滤的未知地点",我们都会记录一个计数器,指示它包含的未知地点的数量。

我尝试用两个for循环来解决这个问题;首先对未知位置进行循环,然后在for循环中对过滤后的未知位置进行循环,见下文。


accuracy = 0.2 #km
df_unknown_places_filtered = pd.DataFrame(columns = ['GpsLatitude', 'GpsLongitude', 'Count'])

for i, row in df_unknown_places.iterrows():
min_dist = 999999
closest = 0
for j, row2 in df_unknown_places_filtered.iterrows():
dist = self.distance(row['GpsLatitude'], row['GpsLongitude'], row2['GpsLatitude'], row2['GpsLongitude'])
if dist < min_dist:
min_dist = dist
closest = j
if min_dist < accuracy:
current_count = df_unknown_places_filtered.at[closest, 'Count'] 
df_unknown_places_filtered.at[closest,'Count'] = current_count + 1
else:
row_to_insert = {'GpsLatitude':row['GpsLatitude'],
'GpsLongitude':row['GpsLongitude'],
'Count': 1                                                           
}
df_unknown_places_filtered = pd.concat([df_unknown_places_filtered, pd.DataFrame.from_records([row_to_insert])], axis = 0)

然而,对于第二次迭代,j的值似乎没有更新,我不知道为什么。有人知道我哪里做错了吗?

您的df_unknown_places_filtered没有行

最新更新