title: Python: How to replace array with discretized (binned) values, not the bin numbers?
body: <p>Suppose I have an array <code>np.array([1,2,4,8,12,16,20,48])</code>. I want to replace the values by the nearest quantiles. So for example, the quartiles (0%,25%, 50%, 75%) are <code>[1,3.5,10,15.6]</code>. I want to bin the array values by the quartile values and then replace the array values by the quartile bin values to which they belong. In the result of <code>np.digitize</code> is <code>[0,0,1,1,2,3,3]</code>. I want to then replace the array as <code>[1,1,3.5,3.5,10,10,15.6,15.6]</code></p>
  
  <p>The thing is, I have HUGE arrays and so I <strong>can't</strong> spend the time with a for-style loop to replace the values one at a time, such as:</p>
  
  <pre><code>data=np.array([1,2,4,8,12,16,20,48])
  bins=[1,3.5,10,15.6]
  newdata=[bins[_] for _ in np.digitize(data,bins[1:])]
  </code></pre>
  
  <p>Anyone know of a clever way to do this?</p>
  
  <p>EDIT and SOLUTION: change the above to:</p>
  
  <pre><code>bins=np.array([1,3.5,10,15.6]
  newdata=bins[np.digitize(data,bins[1:])]
  </code></pre>