I have a pandas dataframe with x,y columns containing coordinates. These coordinates are ground lidar point cloud coordinates. I would like to find the scan position coordinate of x closest to 1000 and y coordinate closest to 2000 ( original scan position which was set up to X = 1000, Y = 2000 during the scanning). Here is the example data (h - height, i - intensity):
x y h i 0 1088.347856 1877.719005 94.869 0.0 1 1083.338856 1886.024105 95.404 2566.0 2 1078.758756 1890.846705 93.314 0.0 3 1078.781556 1890.810205 93.479 257.0 4 1078.791256 1890.804605 93.559 1026.0 5 1078.804756 1890.778805 93.729 1540.0 6 1078.900756 1890.792905 94.324 1283.0 7 1078.900756 1890.792905 94.579 0.0 8 1078.919556 1890.771805 94.749 513.0 9 1078.892856 1890.663205 95.934 770.0 10 1078.841956 1890.731405 95.259 1026.0
I did so far, tried to find closest coordinates:
for x_min in df['x']:
for y_min in df['y']:
x_min = min(df.iloc[:,0], key=lambda x:abs(x-1000))
y_min = min(df.iloc[:,1], key=lambda y:abs(y-2000))
This gave me around 100 X,Y values closest to 1000 and 2000. Now I want to find index values of these numbers.
item_index_x = np.where(df.iloc[:,0]==x_min)
item_index_y = np.where(df.iloc[:,1]==y_min)
Here I tried to find if there are any X,Y coordinates in the same row, not sure if below code is correct. if item_index_x == item_index_y: print(item_index_x) print(item_index_y)
Unfortunately, it didn't give any coordinate closest to 1000, 2000 in the same row. But it found coordinates with X closest to 1000 and Y closest to 2000 independently, meaning for example X = 1000.00001 but Y = 1998.0023. I was wondering if there is anyone who can help me.