I have grid of point and want to calculate number of red and blue point surrounded by each point
for example
For point( X marked )
![[Image: GemLj.png]](https://i.stack.imgur.com/GemLj.png)
blue points = 6
red points = 2
I want to calculate it for the whole data set
Note: I have several dataframes with different grid sizes (24*5, 12*8, etc)
Note: In some of the dataframes I have less number of points then the grid size for example grid is 24*5 but number of points are 118
red=r=1
blue=b=0
Final goal is to have df with
for example
For point( X marked )
![[Image: GemLj.png]](https://i.stack.imgur.com/GemLj.png)
blue points = 6
red points = 2
I want to calculate it for the whole data set
Note: I have several dataframes with different grid sizes (24*5, 12*8, etc)
Note: In some of the dataframes I have less number of points then the grid size for example grid is 24*5 but number of points are 118
red=r=1
blue=b=0
df y x r/b 4 2 0 6 12 1 10 8 1 11 5 1 7 1 0 13 5 0 7 9 1 5 4 0 7 8 1 3 16 1 15 10 1 15 8 1 3 18 0 1 16 0 11 8 1 11 2 1 1 4 0 11 14 1 11 6 1 9 20 0 7 2 1 7 5 0 6 19 0 7 23 0 3 19 0 8 2 0 9 8 1 11 9 0 11 3 0 13 15 1 2 2 0 1 2 0 6 1 0 3 3 0 11 19 0 4 1 0 3 21 0 3 17 0 9 21 0 7 21 0 15 15 1 9 23 0 14 14 0 16 14 0 12 14 0 4 22 1 2 20 0 12 20 0 6 2 0 3 23 0 14 22 1 11 22 0 5 6 0 15 22 0 7 16 0 15 18 0 5 20 0 7 6 0 7 24 0 11 24 0 3 8 1 1 20 0 11 15 0 7 10 1 14 20 0 14 9 0 12 3 0 14 2 0 10 6 0 2 8 0 16 3 0 2 14 0 12 19 0 16 21 0 16 9 0 2 21 0 10 7 0 14 3 0 14 15 0 15 17 1 11 18 1 13 19 1 3 1 0 5 5 0 8 14 1 1 10 1 6 6 0 1 13 0 13 9 0 15 2 0 9 11 1 3 12 1 12 18 0 5 3 0 9 3 1 1 5 0 9 7 0 7 11 1 8 21 0 9 17 0 4 12 0 7 18 0 3 2 0 10 2 0 6 22 0 13 2 0 3 4 0 3 22 0 5 18 1 13 14 1 11 16 1 12 24 1 16 20 0 14 18 1 4 16 0 2 22 1 9 22 0 1 22 0 10 21 0 8 23 0 2 19 0 12 1 0 12 10 1 2 13 0 10 15 1 10 19 0 14 10 0 6 9 0 4 7 0 4 3 0 10 10 0 4 10 1 2 10 1 10 11 0 2 12 0 4 19 0 10 1 0 4 15 0 16 8 0 4 8 0 2 6 0 12 4 0 2 4 0 11 4 0 13 20 0 1 24 0 15 20 0 7 22 0 9 24 0 6 8 0 10 5 0 8 8 1 10 12 1 6 11 0 10 9 0 4 11 0 12 23 0 12 17 0 4 17 0 12 13 0 12 5 1 2 15 0 12 9 0 8 1 0 6 7 0 16 11 0 10 23 0 4 4 0 3 24 0 9 12 1 16 4 0 13 6 1 9 2 1 3 20 0 15 4 0 15 14 1 13 16 1 8 4 0 5 16 0 7 20 0 1 8 0 5 24 0 9 16 1 16 6 0 13 12 0 15 16 1 5 8 1 13 18 0 11 10 1 13 22 1 4 6 0 1 18 0 9 18 0 13 17 1 13 21 0 11 23 0 1 15 0 5 21 0 5 23 0 11 21 1 9 19 0 12 11 0 14 17 0 2 11 0 8 11 0 16 12 0 2 5 0 16 1 1 16 15 0 14 1 0 8 17 0 8 7 0 4 23 0 6 3 0 14 7 0 8 15 0 12 21 0 8 13 0 2 9 0 8 9 0 16 5 0 14 11 0 2 23 0 14 5 1 6 5 0 6 13 0 4 5 0 6 23 0 8 5 0 16 7 0 16 17 1 4 14 0 12 8 0 12 12 0 6 10 1 14 12 0 16 10 0 8 12 1 14 8 0 1 19 0 5 17 0 3 15 0 9 15 0 3 13 0 11 13 1 13 13 0 13 7 1 11 7 1 13 1 1 16 16 1 10 14 1 12 22 0 6 18 0 9 4 0 3 6 1 7 14 0 3 14 0 8 6 0 5 12 1 5 11 0 6 14 1 10 16 0 4 20 1 16 18 1 8 18 0 4 24 0 6 16 1 14 16 1 13 3 0 15 11 0 15 1 0 15 3 0 5 9 0 1 17 0 1 9 0 1 1 0 7 17 0 1 21 0 10 18 0 16 22 1 6 24 0 4 18 0 2 16 0 16 2 0 15 6 0 9 14 1 5 14 1 1 14 1 3 10 0 9 10 0 8 16 0 5 10 1 9 6 0 9 13 0 15 12 0 8 20 0 1 12 0 11 12 1 8 22 0 7 12 1 13 4 0 8 24 0 13 10 1 6 20 0 8 10 1 1 6 0 5 22 0 5 2 0 2 1 0 11 11 0 15 7 0 11 1 1 9 5 1 7 7 0 15 13 1 13 11 1 5 7 0 15 21 0 10 22 1 12 2 0 14 4 0 2 24 0 12 16 1 2 18 0 15 19 0 3 11 0 5 19 0 10 24 0 11 17 0 1 23 0 3 7 0 2 17 0 4 21 0 14 13 0 6 15 0 7 13 0 10 4 0 6 21 0 6 17 0 14 21 0 16 13 0 10 13 0 15 5 0 10 17 0 5 15 0 7 3 0 2 7 0 4 13 0 3 9 1 10 3 0 15 9 0 2 3 0 12 15 0 16 19 0 7 15 0 12 6 0 8 19 0 4 9 0 5 1 0 1 7 0 1 3 0 9 1 0 5 13 0 1 11 0 9 9 0 12 7 0 13 8 1 14 6 0 15 23 0 16 23 0 15 24 0 16 24 0 13 23 1 14 23 1 13 24 1 14 24 1 7 4 0 11 20 1 6 4 1 10 20 1 8 3 1 14 19 0 3 5 1 7 19 1
color_matrix=df["r/b"].values.reshape(24, 16) #since this dataframe has grid size 24*16 from scipy import signal adjacent_boxes = np.array([[1,1,1], [1,0,1], [1,1,1]]) red_dots = signal.convolve2d(color_matrix, adjacent_boxes , mode='same', boundary='fill') blue_dots = signal.convolve2d(1 - color_matrix, adjacent_boxes , mode='same', boundary='fill') df['red_dots'] = red_dots.flatten() df['blue_dots'] = blue_dots.flatten()this code has issue as 1st col= y axis, 2nd col= x axis, show the coordinates of each point and they are not in the sequence , but simply doing .reshape(num_rows, num_cols) will create 2D array as it is sequenced in the dataframa (col3)
Final goal is to have df with
y x r/b red_dots blue_dots