title: Tabular data structure modification in python
body: <p>My query had two parts. So, I have a data in the following format, where my target field is ‘Groups’.
  Stage 1:</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th>ID</th>
  <th>Income</th>
  <th>Owns_Car</th>
  <th>Family_size</th>
  <th>Groups</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td>1</td>
  <td>65153</td>
  <td>Yes</td>
  <td>1</td>
  <td>&quot;A&quot;,&quot;C&quot;,&quot;D&quot;</td>
  </tr>
  <tr>
  <td>2</td>
  <td>78965</td>
  <td>No</td>
  <td>2</td>
  <td>&quot;B&quot;,&quot;D&quot;,&quot;E&quot;</td>
  </tr>
  <tr>
  <td>3</td>
  <td>65465</td>
  <td>Yes</td>
  <td>5</td>
  <td>&quot;A&quot;,&quot;D&quot;,&quot;F&quot;</td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>I would want to build a multinomial classification model, for which I want to have the data formatted like this.</p>
  <p>Stage 2:</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th>ID</th>
  <th>Income</th>
  <th>Owns_car</th>
  <th>Family_size</th>
  <th>Groups</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td>1</td>
  <td>65153</td>
  <td>Yes</td>
  <td>1</td>
  <td>A</td>
  </tr>
  <tr>
  <td>1</td>
  <td>65153</td>
  <td>Yes</td>
  <td>1</td>
  <td>C</td>
  </tr>
  <tr>
  <td>1</td>
  <td>65153</td>
  <td>Yes</td>
  <td>1</td>
  <td>D</td>
  </tr>
  <tr>
  <td>2</td>
  <td>78965</td>
  <td>No</td>
  <td>2</td>
  <td>B</td>
  </tr>
  <tr>
  <td>2</td>
  <td>78965</td>
  <td>No</td>
  <td>2</td>
  <td>D</td>
  </tr>
  <tr>
  <td>2</td>
  <td>78965</td>
  <td>No</td>
  <td>2</td>
  <td>E</td>
  </tr>
  <tr>
  <td>3</td>
  <td>65465</td>
  <td>Yes</td>
  <td>5</td>
  <td>A</td>
  </tr>
  <tr>
  <td>3</td>
  <td>65465</td>
  <td>Yes</td>
  <td>5</td>
  <td>D</td>
  </tr>
  <tr>
  <td>3</td>
  <td>65465</td>
  <td>Yes</td>
  <td>5</td>
  <td>F</td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>Once I have built the model and have assigned probabilities, I would like to assign the probability into a group per ID like this.</p>
  <p>Stage 3:</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th>ID</th>
  <th>Income</th>
  <th>Owns_Car</th>
  <th>Family_size</th>
  <th>Groups</th>
  <th>Probability</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td>1</td>
  <td>65153</td>
  <td>Yes</td>
  <td>1</td>
  <td>&quot;A&quot;,&quot;C&quot;,&quot;D&quot;</td>
  <td>1,0,0</td>
  </tr>
  <tr>
  <td>2</td>
  <td>78965</td>
  <td>No</td>
  <td>2</td>
  <td>&quot;B&quot;,&quot;D&quot;,&quot;E&quot;</td>
  <td>1,1,0</td>
  </tr>
  <tr>
  <td>3</td>
  <td>65465</td>
  <td>Yes</td>
  <td>5</td>
  <td>&quot;A&quot;,&quot;D&quot;,&quot;F&quot;</td>
  <td>1,0,1</td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>I am not being able to build the loop or function in order to go from stage 1 to stage 2 and back to stage 3. any tips or code snippets for anything done similar?</p>