title: How to convert group.iterrows() loop result to dataframe in Pandas
body: <p>Consider below data:</p>
  <pre><code>df['rank']=df.groupby('Contact').cumcount()
  df.set_index('rank', inplace=True, drop=True)               
  df
  </code></pre>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th>Contact</th>
  <th>New</th>
  <th>Old</th>
  <th>DateNew</th>
  <th>DateOld</th>
  <th>moddate</th>
  <th>User</th>
  <th>Rank</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td>0</td>
  <td>mary</td>
  <td>False</td>
  <td>True</td>
  <td>2021-01-12 11:00:00</td>
  <td>NaT</td>
  <td>2021-01-12 12:48:00</td>
  <td>sys</td>
  </tr>
  <tr>
  <td>1</td>
  <td>mary</td>
  <td>True</td>
  <td>False</td>
  <td>NaT</td>
  <td>2021-01-12 11:00:00</td>
  <td>2021-01-12 12:47:00</td>
  <td>Rob</td>
  </tr>
  <tr>
  <td>2</td>
  <td>mary</td>
  <td>False</td>
  <td>True</td>
  <td>2021-01-12 11:00:00</td>
  <td>NaT</td>
  <td>2021-01-12 12:45:00</td>
  <td>sys</td>
  </tr>
  <tr>
  <td>3</td>
  <td>mary</td>
  <td>True</td>
  <td>False</td>
  <td>NaT</td>
  <td>2021-11-29 11:00:00</td>
  <td>2021-01-12 12:44:00</td>
  <td>Rob</td>
  </tr>
  <tr>
  <td>0</td>
  <td>john</td>
  <td>True</td>
  <td>False</td>
  <td>NaT</td>
  <td>2021-12-10 11:00:00</td>
  <td>2021-12-10 16:15:00</td>
  <td>Tina</td>
  </tr>
  <tr>
  <td>1</td>
  <td>john</td>
  <td>False</td>
  <td>True</td>
  <td>2021-12-10 11:00:00</td>
  <td>NaT</td>
  <td>2021-12-10 15:30:00</td>
  <td>sys</td>
  </tr>
  <tr>
  <td>2</td>
  <td>john</td>
  <td>True</td>
  <td>False</td>
  <td>NaT</td>
  <td>2021-12-10 11:00:00</td>
  <td>2021-12-10 15:29:00</td>
  <td>Rina</td>
  </tr>
  <tr>
  <td>3</td>
  <td>john</td>
  <td>False</td>
  <td>True</td>
  <td>2021-12-10 11:00:00</td>
  <td>NaT</td>
  <td>2021-12-10 12:37:00</td>
  <td>sys</td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>How do I convert the below result to dataframe? <code>df</code> is a <code>pandas.core.frame.DataFrame</code> and <code>grp</code> is a <code>pandas.core.groupby.generic.DataFrameGroupBy</code>.</p>
  <pre><code>grp = df.groupby('Contact')
  for name,group in grp:
          for i,row in group.iterrows():
              
              if i &lt; len(group)-1:
                  if (group.loc[i,'Old'] == group.loc[i+1,'New']):
                      if group.loc[i,'User'] == 'sys':
                          if group.loc[i+1,'User'] !='sys':
                              group.loc[i,'Overwritten']= True
                              
                          else:
                              group.loc[i,'Overwritten']= False
                      else:
                          group.loc[i,'Overwritten']= False
                                
                  else:
                      group.loc[i,'Overwritten']= False
      
          print(group)
  </code></pre>
  <p>The above solution may not be the best approach, maybe I can use <code>shift</code> function. But I would like to know how I can get the <code>groupby</code> results in the loop as data frame so I can proceed with more filtrations/analysis. I am still kind of newbie in Pandas.
  Thanks.</p>