title: Repeat rows based on numbers in multiple columns - Python
body: <p>I have a lot of data that I'm trying to do some basic machine learning on, kind of like the Titanic example that predicts whether a passenger survived or died (I learned this in an intro Python class) based on factors like their gender, age, fare class...</p>
  <p>What I'm trying to predict is whether a screw fails depending on how it was made (referred to as Lot). The engineers just listed how many times a failure occurred. Here's how it's formatted.</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th style="text-align: center;">Lot</th>
  <th style="text-align: center;">Failed?</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td style="text-align: center;">100</td>
  <td style="text-align: center;">3</td>
  </tr>
  <tr>
  <td style="text-align: center;">110</td>
  <td style="text-align: center;">0</td>
  </tr>
  <tr>
  <td style="text-align: center;">120</td>
  <td style="text-align: center;">1</td>
  </tr>
  <tr>
  <td style="text-align: center;">130</td>
  <td style="text-align: center;">4</td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>The values in the cells are the number of occurrences, so for example:</p>
  <p>Lot 100 had three screws that failed</p>
  <p>Lot 110 had 0 screws that failed</p>
  <p>Lot 120 had one screw that failed</p>
  <p>Lot 130 had four screws that failed</p>
  <p>I plan on doing a <code>logistic regression</code> using <code>scikit-learn</code>, but first I need each row to be listed as a failure or not. What I'd like to see is a row for every observation, and have them listed as either a <code>0</code> (did not occur) or <code>1</code> (did occur). Here's what it'd look like after</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th>Lot</th>
  <th>Failed?</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td>100</td>
  <td>1</td>
  </tr>
  <tr>
  <td>100</td>
  <td>1</td>
  </tr>
  <tr>
  <td>100</td>
  <td>1</td>
  </tr>
  <tr>
  <td>110</td>
  <td>0</td>
  </tr>
  <tr>
  <td>120</td>
  <td>1</td>
  </tr>
  <tr>
  <td>140</td>
  <td>1</td>
  </tr>
  <tr>
  <td>140</td>
  <td>1</td>
  </tr>
  <tr>
  <td>140</td>
  <td>1</td>
  </tr>
  <tr>
  <td>140</td>
  <td>1</td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>Here's what I've tried and what I've gotten</p>
  <pre><code>df = pd.DataFrame({
  'Lot' : ['100', '110', '120', '130'],
  'Failed?' : [3, 0, 1, 4]
  }) 
  
  df.loc[df.index.repeat(df['Failed?'])].reset_index(drop = True)
  </code></pre>
  <p>When I do this it repeats the rows but keeps the same values in the <code>Failed?</code> column.</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th>Lot</th>
  <th>Failed?</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td>100</td>
  <td>3</td>
  </tr>
  <tr>
  <td>100</td>
  <td>3</td>
  </tr>
  <tr>
  <td>100</td>
  <td>3</td>
  </tr>
  <tr>
  <td>110</td>
  <td>0</td>
  </tr>
  <tr>
  <td>120</td>
  <td>1</td>
  </tr>
  <tr>
  <td>140</td>
  <td>4</td>
  </tr>
  <tr>
  <td>140</td>
  <td>4</td>
  </tr>
  <tr>
  <td>140</td>
  <td>4</td>
  </tr>
  <tr>
  <td>140</td>
  <td>4</td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>Any ideas? Thank you!</p>