title: Python/Pandas find closest value above/below in one Column
body: <p>I have a large data frame (~1 million rows). I am ultimately going to be interpolating spectra between two age groups. However I need to first find the nearest values above and below any age I need to find. </p>
  
  <p>The DataFrame briefly looks like this</p>
  
  <pre><code> Age   Wavelength   Luminosity
   1     
   1
   1
   4
   4
   6
   6
  </code></pre>
  
  <p>I need to be able to input 5, and return values 4 and 6. I am struggling to find  a way to do this? This is what I tried :</p>
  
  <pre><code>def findnearest(array,value):
      idx = np.searchsorted(array,value, side='left')
      if idx &gt; 125893.0:
          return array[idx]
      else:
          return array[idx]
      idx1 = np.searchsorted(array,value, side='right')
      if idx1 &lt; 2e10:
          return array[idx1]
      else:
          return array [idx1-1]
  
  C = findnearest(m05_010['age'], 5.12e7)
  
  print(C)
  </code></pre>
  
  <p>This only returns one value, and not both. Is this the right path or should I be doing something different? Is there a better way?</p>