pandas find closest value from other column to entire column

Question:

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!

Asked By: Ofek Glick

||

Answers:

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)]

Update – i.e., avoid the loop

l2 = list(zip(df.index, df['X'].sub([df['Y'].values]*len(df)).abs().agg(np.argmin)))
# [(0, 2), (1, 1), (2, 3), (3, 4), (4, 4)]
Answered By: It_is_Chris
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.