title: iteratively retrieving information contained in different rows in pandas dataframe
body: <p>I have a dataframe which contains post and comments.
  Every comment has an id and a parent id (identifies the comment or post which the comment was a response to).
  Posts only have an id, since they don't answer to anything.</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th>submission</th>
  <th>id</th>
  <th>parent id</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td>post1</td>
  <td>1</td>
  <td></td>
  </tr>
  <tr>
  <td>comment1</td>
  <td>2</td>
  <td>1</td>
  </tr>
  <tr>
  <td>comment2</td>
  <td>3</td>
  <td>1</td>
  </tr>
  <tr>
  <td>comment3</td>
  <td>4</td>
  <td>2</td>
  </tr>
  <tr>
  <td>comment4</td>
  <td>5</td>
  <td>4</td>
  </tr>
  <tr>
  <td>post2</td>
  <td>6</td>
  <td></td>
  </tr>
  <tr>
  <td>comment5</td>
  <td>7</td>
  <td>6</td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>I would like to retrieve the id of the original post for every comment and obtain something like that:</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th>submission</th>
  <th>id</th>
  <th>parent id</th>
  <th>ancestor id</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td>post1</td>
  <td>1</td>
  <td></td>
  <td></td>
  </tr>
  <tr>
  <td>comment1</td>
  <td>2</td>
  <td>1</td>
  <td>1</td>
  </tr>
  <tr>
  <td>comment2</td>
  <td>3</td>
  <td>1</td>
  <td>1</td>
  </tr>
  <tr>
  <td>comment3</td>
  <td>4</td>
  <td>2</td>
  <td>1</td>
  </tr>
  <tr>
  <td>comment4</td>
  <td>5</td>
  <td>4</td>
  <td>1</td>
  </tr>
  <tr>
  <td>post2</td>
  <td>6</td>
  <td></td>
  <td></td>
  </tr>
  <tr>
  <td>comment5</td>
  <td>7</td>
  <td>6</td>
  <td>2</td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>to do so I tried to loop from the end of the dataframe to the beginning, iteratively tracing back the parent_id of the parent_id until I found an empty parent_id cell.
  On the test dataframe it works, but on the main one is too slow. Is there a way to make it more efficient?</p>
  <p>Here my original code:</p>
  <pre><code>#creating a column for the id of the original post
  df[&quot;ancestor&quot;] = df.id
  #obtaining the id of the original post for every comment
  for i in reversed(range(len(df.id))): #looping trough the comments
    id = df[&quot;parent_id&quot;][i] #variable to initialize the future loop
    parent = id
    while parent != &quot;&quot;: #only looping trough comments
      df.ancestor[i] = id
      parent = df.parent_id[df.id == id].values[0]
      id = parent
  </code></pre>