title: Downsample pandas data frame based on count column
body: <p>I have a thousands of data frame like the following, though much larger (1000000 rows, 100 columns).</p>
  
  <pre><code>data = pd.DataFrame({'cols1':[4, 5, 5, 4, 321, 32, 5],
                       'count':[45, 66, 6, 6, 1, 432, 3],
                       'Value':['Apple', 'Boy', 'Car', 'Corn', 'Anne', 'Barnes', 'Bayesian']})
  </code></pre>
  
  <p>I want to randomly sample from this data frame and make a new data frame such that the sum of count should only equal N. Meaning I want to randomly sample based on the count value as a weight, and make a new data frame with this new resampled data such that sum of count is N.</p>
  
  <p>The relative proportions should stay approximately the same, and no value when resampled should exceed the count of the original count value. The values in cols1 (or any other column except Value and count) should remain the same. </p>
  
  <p>For example, if N was 50, it might look like:</p>
  
  <pre><code>pd.DataFrame({'cols1':[4, 5, 5, 4, 321, 32, 5],
                       'count':[4, 7, 1, 1, 0, 37, 0],
                       'Value':['Apple', 'Boy', 'Car', 'Corn', 'Anne', 'Barnes', 'Bayesian']})
  </code></pre>
  
  <p>How can this be done?</p>
  
  <p>Efficiency is key, otherwise I could expand the data frame based on count and randomly sample without replacement, then merge it back together. </p>
  
  <p>Thanks,
  Jack</p>