title: Find nearest value in dataframe?
body: <p>Let's say I have a dataframe like below,</p>
  <pre><code>     0           1               2               3               4
  0   (989, 998)  (1074, 999)     (1159, 1000)    (1244, 1001)    (1329, 1002)
  1   (970, 1042) (1057, 1043)    (1143, 1044)    (1230, 1045)    (1316, 1046)
  2   (951, 1088) (1039, 1089)    (1127, 1090)    (1214, 1091)    (1302, 1092)
  3   (930, 1137) (1020, 1138)    (1109, 1139)    (1198, 1140)    (1287, 1141)
  4   (909, 1188) (1000, 1189)    (1091, 1190)    (1181, 1191)    (1271, 1192)
  </code></pre>
  <p>Each cell has x and y coordinates in tuple. I have an input called I and that's also x and Y coordinate in a tuple. My goal is to find the nearest point for input I.</p>
  <p>Sample Input:</p>
  <p><code>(1080, 1000)</code></p>
  <p>Sample Output:</p>
  <p><code>(1074, 999)</code></p>
  <p>I have tried the below snippet.</p>
  <pre><code>def find_nearest(array, key):
      min_ = 1000
      a = 0
      b = 0
      for item in array:
          diff = abs(item[0]-key[0])+abs(item[1]-key[1])
          if diff&lt;min_:
              min_ = diff
              a,b = item
          if diff==0:
              return (a,b)
      return (a,b)
  find_nearest(sum(df.values.tolist(), []), I)
  </code></pre>
  <p>This gives me what I expected. But, Is there any efficient solution for the problem?</p>