title: Search and match data from two files in Python
body: <p>I have two different files; A reference file and a data sets with different length.</p>
  
  <pre><code>A reference file ("location.dat") contains:
  40505   5.0666667   102.2166667
  40517   5.6833333   101.8500000
  40586   5.7666667   102.2000000
  40587   5.8166667   102.0500000
  40663   6.0333333   102.1166667
  41525   5.5500000   100.4833333
  41529   5.3500000   100.4000000 
  ...............
  ...............
  
  A data sets ("input.dat") contains:
  40517   2014    12  18  0   17.4
  40586   2014    12  18  0   9.9
  40587   2014    12  18  0   15.5
  40663   2014    12  18  0   30.9
  41525   2014    12  18  0   0
  41529   2014    12  18  0   0
  41540   2014    12  18  0   0
  41543   2014    12  18  0   0
  41548   2014    12  18  0   0
  41549   2014    12  18  0   0
  41551   2014    12  18  0   0
  41610   2014    12  18  0   0
  </code></pre>
  
  <p>Question:
  How to search and match data set so that the output file will combine certain selected values from both files like this:</p>
  
  <pre><code>output.dat
  40517   5.6833333   101.8500000 17.4
  40586   5.7666667   102.2000000 9.9
  40587   5.8166667   102.0500000 15.5
  ............
  ...........
  
  The current script is:
  
  data1=np.loadtxt('location.dat')
     lats1=data1[:,1]
     lons1=data1[:,2]
     code1=data1[:,0]
  
      data2=np.loadtxt('input.dat')
      rain=data2[:,5]
      code2=data2[:,0]
  
      ind=[]
      for i in range(len(data1)):
         dist=code1[i]
         ind.append(np.where(dist==np.int(dist))[0][0])
         rain2=rain[ind]
  
      data3=np.array([code1,lats1,lons1,rain2])
      data3=np.transpose(data3)
      np.savetxt('output.dat',data3,fmt='%9.3f')
  </code></pre>
  
  <p>The current result </p>
  
  <pre><code>40517.000     5.683   101.850     0.000
  40586.000     5.767   102.200     0.000
  40587.000     5.817   102.050     0.000
  40663.000     6.033   102.117     0.000
  41525.000     5.550   100.483     0.000
  41529.000     5.350   100.400     0.000
  41540.000     5.383   100.550     0.000
  </code></pre>
  
  <p>The rain2 values did not properly append from the input file. How to convert the first column output to be integer?.Any ideas of what went wrong??.TQ</p>