title: pandas: bin data into specific number of bins of specific size
body: <p>I would like to bin a dataframe by the values in a single column into bins of a specific size and number.</p>
  
  <p>Here is an example df:</p>
  
  <pre><code>df= pd.DataFrame(np.random.randint(0,10000,size=(10000, 4)), columns=list('ABCD'))
  </code></pre>
  
  <p>Say I want to bin by column D, I will first sort the data:</p>
  
  <pre><code>df.sort('D')
  </code></pre>
  
  <p>I would now wish to bin so that the first if bin size is 50 and bin number is 100, the first 50 values will go into bin 1, the next into bin 2, and so on and so forth. Any remaining values after the twenty bins should all go into the final bin. Is there anyway of doing this?</p>
  
  <p>EDIT:</p>
  
  <p>Here is a sample input:</p>
  
  <pre><code>x = pd.DataFrame(np.random.randint(0,10,size=(10, 4)), columns=list('ABCD'))
  </code></pre>
  
  <p>And here is the expected output:</p>
  
  <pre><code>    A   B   C   D   bin
  0   6   8   6   5   3
  1   5   4   9   1   1
  2   5   1   7   4   3
  3   6   3   3   3   2
  4   2   5   9   3   2
  5   2   5   1   3   2
  6   0   1   1   0   1
  7   3   9   5   8   3
  8   2   4   0   1   1
  9   6   4   5   6   3
  </code></pre>
  
  <p>As an extra aside, is it also possible to bin any equal values in the same bin? So for example, say I have bin 1 which contains values, <code>0,1,1</code> and then bin 2 contains <code>1,1,2</code>. Is there any way of putting those two 1 values in bin 2 into bin 1? This will create very uneven bin sizes but this is not an issue.</p>