title: Pandas sampling every nth value, or closest matching value to nth value
body: <p>I have a bit of a peculiar problem. I have a very large dataframe that I'm trying to sample.</p>
  
  <p>My data looks something like this (two columns x and y):</p>
  
  <pre><code>x,y
  1,'a'
  1,'b'
  1,'c'
  3,'a'
  3,'b'
  3,'c'
  6,'a'
  6,'b'
  6,'c'
  </code></pre>
  
  <p>I want to sample based on the value of 'x'. I'd like one sample out of every 32 x values (based on the change in x, so I want to sample once between 1 and 32, even if there is only one value of x between 1 and 32).</p>
  
  <p>I wanted to do something like: <code>df[df['x'] % 32 == 0]</code>. The problem is that the values of x are not necessarily evenly spaced (they are "approximately" though, but not always -- such as in the example given). For example, if all the values of x in a particular subproblem are odd, the above sampling will return an empty dataframe. Effectively, I want to sample once every 32 'x's, or take the closest value to the 32nd value (it is acceptable to approximate closest -- for example, taking the next available value is fine) if no such value exists.</p>
  
  <p>So if I had a series of X's <code>[0, 10, 32, 39, 64, 70, 73, 74, 97, 100, 110, 129]</code>, I'd like to sample the rows with x values matching <code>[0, 32, 64, 97, 129]</code>.</p>
  
  <p>Ideally, I could also vectorize this operation, as the dataframes are often quite large.</p>