title: Block randomization using python?
body: <p>I have the following input table (df):</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th>ColumnA</th>
  <th>ColumnB</th>
  <th>Blocks</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td>A</td>
  <td>12</td>
  <td>1</td>
  </tr>
  <tr>
  <td>B</td>
  <td>32</td>
  <td>1</td>
  </tr>
  <tr>
  <td>C</td>
  <td>44</td>
  <td>1</td>
  </tr>
  <tr>
  <td>D</td>
  <td>76</td>
  <td>2</td>
  </tr>
  <tr>
  <td>E</td>
  <td>99</td>
  <td>2</td>
  </tr>
  <tr>
  <td>F</td>
  <td>123</td>
  <td>2</td>
  </tr>
  <tr>
  <td>G</td>
  <td>65</td>
  <td>2</td>
  </tr>
  <tr>
  <td>H</td>
  <td>87</td>
  <td>3</td>
  </tr>
  <tr>
  <td>I</td>
  <td>76</td>
  <td>3</td>
  </tr>
  <tr>
  <td>J</td>
  <td>231</td>
  <td>3</td>
  </tr>
  <tr>
  <td>k</td>
  <td>80</td>
  <td>4</td>
  </tr>
  <tr>
  <td>l</td>
  <td>55</td>
  <td>4</td>
  </tr>
  <tr>
  <td>m</td>
  <td>27</td>
  <td>5</td>
  </tr>
  <tr>
  <td>n</td>
  <td>67</td>
  <td>5</td>
  </tr>
  <tr>
  <td>o</td>
  <td>34</td>
  <td>5</td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>I would like to perform block randomization such that, it pick one value from each blocks ( one value from 1,2,3,4,5) and create that as a separate table.</p>
  <p>The output should look something like the following:</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th>ColumnA</th>
  <th>ColumnB</th>
  <th>Blocks</th>
  <th>Groups</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td>B</td>
  <td>32</td>
  <td>1</td>
  <td>A1</td>
  </tr>
  <tr>
  <td>E</td>
  <td>99</td>
  <td>2</td>
  <td>A1</td>
  </tr>
  <tr>
  <td>I</td>
  <td>76</td>
  <td>3</td>
  <td>A1</td>
  </tr>
  <tr>
  <td>l</td>
  <td>55</td>
  <td>4</td>
  <td>A1</td>
  </tr>
  <tr>
  <td>m</td>
  <td>27</td>
  <td>5</td>
  <td>A1</td>
  </tr>
  <tr>
  <td>A</td>
  <td>12</td>
  <td>1</td>
  <td>A2</td>
  </tr>
  <tr>
  <td>F</td>
  <td>123</td>
  <td>2</td>
  <td>A2</td>
  </tr>
  <tr>
  <td>k</td>
  <td>80</td>
  <td>3</td>
  <td>A2</td>
  </tr>
  <tr>
  <td>m</td>
  <td>27</td>
  <td>4</td>
  <td>A2</td>
  </tr>
  <tr>
  <td>n</td>
  <td>67</td>
  <td>5</td>
  <td>A2</td>
  </tr>
  <tr>
  <td>C</td>
  <td>44</td>
  <td>1</td>
  <td>A3</td>
  </tr>
  <tr>
  <td>H</td>
  <td>87</td>
  <td>2</td>
  <td>A3</td>
  </tr>
  <tr>
  <td>J</td>
  <td>231</td>
  <td>3</td>
  <td>A3</td>
  </tr>
  <tr>
  <td>n</td>
  <td>67</td>
  <td>4</td>
  <td>A3</td>
  </tr>
  <tr>
  <td>o</td>
  <td>34</td>
  <td>5</td>
  <td>A4</td>
  </tr>
  <tr>
  <td>D</td>
  <td>76</td>
  <td>1</td>
  <td>A4</td>
  </tr>
  <tr>
  <td>G</td>
  <td>65</td>
  <td>2</td>
  <td>A4</td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>Randomly selected rows such that each group has all the blocks (evenly distributed).</p>
  <p>What I tried so far?</p>
  <pre><code>
  df = df.groupby('blocks').apply(lambda x: x.sample(frac=1,random_state=1234)).reset_index(drop=True)
  treatment_groups = [f&quot;A{i}&quot; for i in range(1, n+1)]
  df['Groups'] = (df.index // n).map(dict(zip(idx, treatment_groups)))
  </code></pre>
  <p>This doesn't randomize according to the blocks column. How do I do that?</p>