title: How to subsample a pandas dataframe respecting the frequency of each class?
body: <p>How to subsample a pandas dataframe by taking into account the frequency of each label or category. For instance, consider the class frequency of the following <a href="https://pastebin.com/TqqZ3Q85" rel="nofollow noreferrer">dataset</a>:</p>
  
  <pre><code>Class              freq
  Iris-setosa        50
  Iris-versicolor    50
  Iris-virginica     50
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/wwYbO.png" alt="frequency for each class"></p>
  
  <p>How can I randomly take the 50% of instances for each class, in order to reduce my dataset?. I all ready know that I can do:</p>
  
  <pre><code>df['class'].sample(frac=0.5)
  </code></pre>
  
  <p>or:</p>
  
  <pre><code>df.take(np.random.permutation(len(df))[:5])
  </code></pre>
  
  <p>However, this is not respecting the class frequency of the dataset. Any idea of how to get:</p>
  
  <pre><code>Class              freq
  Iris-setosa        25
  Iris-versicolor    25
  Iris-virginica     25
  </code></pre>