pandas dataframe groupby and return nth row unless nth row doesn't exist
<p>I have a pandas dataframe like so:</p>

<pre><code>    date        id          person                      latitude    longitude
0   2016-07-11  1           rob                         41.395279   2.162126
1   2016-07-11  1           harry                       51.485146   0.041339
2   2016-07-11  1           susan                       51.496457   0.051234 
3   2016-07-11  2           lenny                       48.863281   2.339698
4   2016-07-11  2           wendy                       51.522469   -0.148812
5   2016-07-11  3           john                        51.490746   -0.022011
</code></pre>

<p>I want to group this dataframe by both date and ID, then return the second row of this group for the other three columns. In the case that there is only one row for that group, then I want it to return the first row (i.e. not drop the group).</p>

<p>Currently I have the following but this is dropping occasions where there is only one instance of that group.</p>

<pre><code>df_grouped = df.groupby(['date', 'id']).nth(1).reset_index()
</code></pre>

<p>The output I am aiming for is:</p>

<pre><code>    date        id          person                      latitude    longitude
0   2016-07-11  1           harry                       51.485146   0.041339
1   2016-07-11  2           wendy                       51.522469   -0.148812
2   2016-07-11  3           john                        51.490746   -0.022011
</code></pre>

<p>Any help would be greatly appreciated! Thanks.</p>