Assuming I have the following data frames:
# data frame circles
ID x y
1 4 5
2 5 6
# data frame points
ID x y
1 2 1
2 1 2
I'm going to check if every point is located inside every circle or not And if the point located inside (based on some calculation), take the circle's ID and keep it in a separate list
# output of the list (ID of the circles)
[1]
[1 2]
This means point 1 is inside circle 1 and point 2 is located in both circles.
Now, I wrote the following function to do that locating work.
for i in range(len(points)):
for j in range(len(circles)):
get_point_coordinates = (points.loc[i].at["x"], points.loc[i].at["y"])
get_circle_coordinates = (circles.loc[j].at["x"], circles.loc[j].at["y"])
### calling library's function to calculate
distance = distance.great_circle(get_point_coordinates, get_circle_coordinates ).km
if distance <= 5:
list[i].append(circles.loc[j].at["ID"])
It's a nested loop to iterate every single point and check all the circles one by one.
THE PROBLEM IS: the original data frame is over 100,000 rows, so it takes forever to iterate.
I read some posts about using apply to deal with massive data.
so, I tried the following but it didn't work (error: The truth value of a Series is ambiguous).
for i in range(len(circles)):
newlist = newDataFrame['result'].apply(get_distance_function(circles.loc[i].at["x"], circles.loc[i].at["y"], points['x'], points['y']))
Yet I think it would still be a problem because I got rid of the inner for loop only, I still have to iterate 100,000 times instead of 100,000 * 100,000
So, any better ideas? or this approach is the shortest and I should fix the error?