title: Calculating the minimum distance between two DataFrames
body: <p>I like to find the item of DF2 that is cloest to the item in DF1.</p>
  
  <p>The distance is euclidean distance.</p>
  
  <p>For example, for A in DF1, F in DF2 is the cloeset one.</p>
  
  <pre><code>&gt;&gt;&gt; DF1
     X  Y name
  0  1  2    A
  1  3  4    B
  2  5  6    C
  3  7  8    D
  &gt;&gt;&gt; DF2
     X  Y name
  0  3  8    E
  1  2  4    F
  2  1  9    G
  3  6  4    H
  </code></pre>
  
  <p>My code is </p>
  
  <pre><code>DF1 = pd.DataFrame({'name' : ['A', 'B', 'C', 'D'],'X' : [1,3,5,7],'Y' : [2,4,6,8]})
  DF2 = pd.DataFrame({'name' : ['E', 'F', 'G', 'H'],'X' : [3,2,1,6],'Y' : [8,4,9,4]})
  
  
  def ndis(row):
      try:
          X,Y=row['X'],row['Y']
          DF2['DIS']=(DF2.X-X)*(DF2.X-X)+(DF2.Y-Y)*(DF2.Y-Y)
          temp=DF2.ix[DF2.DIS.idxmin()]
          return temp[2]  #       print temp[2]
      except:
          pass        
  
  
  DF1['Z']=DF1.apply(ndis, axis=1)
  </code></pre>
  
  <p>This works fine, and it will take too long for large data set.</p>
  
  <p>Another question is to how to find the 2nd and 3d cloeset ones.</p>