I have a pandas DataFrame with two columns of numbers:
index | X | Y |
---|---|---|
0 | 0.1 | 0.55 |
1 | 0.2 | 0.2 |
2 | 0.4 | 0.1 |
3 | 0.8 | 0.35 |
4 | 1 | 0.9 |
I want to find tuples of the indexes of the closest values.
For example for this dataframe I would want the result
(0,2), (1,1), (2,3), (3,4), (4,4)
I saw this link explaining how to do this for a given input, but I am wondering if there is a cleaner way to do this for my task. Thanks in Advance!
CodePudding user response:
You can try list comprehension if you are not working with a huge dataset. Subtract (.sub()
) each x value from each y value, get the absolute value (.abs()
) and then get the int position of the smallest value in the Series (.argmin()
).
l = [(idx, df['Y'].sub(x).abs().argmin()) for idx, x in enumerate(df['X'])]
# [(0, 2), (1, 1), (2, 3), (3, 4), (4, 4)]