Home > Software engineering >  Using Pandas Count number of Cells in Column that is within a given radius
Using Pandas Count number of Cells in Column that is within a given radius

Time:11-12

To set up the question. I have a dataframe containing spots and their x,y positions. I want to iterate over each spot and check all other spots to see if they are within a radius. I then want to count the number of spots within the radius in a new column of the dataframe. I would like to iterate over the index as I have a decent understanding on how that works. I know that I am missing something simple but I have not been able to find a solution that works for me yet. Thank you in advance!

radius = 3

df = pd.DataFrame({'spot_id':[1,2,3,4,5],'x_pos':[5,4,10,3,8],'y_pos':[4,10,8,6,3]})

spot_id x_pos y_pos
0   1   5   4
1   2   4   10
2   3   10  8
3   4   3   6
4   5   8   3

I then want to get something that looks like this

spot_id x_pos y_pos spots_within_radius
0   1   5    4      1
1   2   4    10     0
2   3   10   8      0
3   4   3    6      1
4   5   8    3      0

CodePudding user response:

To do it in a vectorized way, you can use Graph of KD Tree method VS distance matrix

For small numbers of points, the distance matrix method is faster. After you get 300 points to compare to each other, the KD Tree is faster. Note that this graph has a log axis on both scales.

Full testing details can be found here.

  • Related