pandas dataframe groupby and get nth row
I think the nth method is supposed to do just that:
In [10]: g = df.groupby('ID')
In [11]: g.nth(1).dropna()
Out[11]:
col1 col2 col3 col4 col5
ID
1 1.1 D 4.7 x/y/z 200
2 3.4 B 3.8 x/u/v 404
3 1.1 A 2.5 x/y/z/n 404
5 2.6 B 4.6 x/y 500
In 0.13 another way to do this is to use cumcount:
df[g.cumcount() == n - 1]
...which is significantly faster.
In [21]: %timeit g.nth(1).dropna()
100 loops, best of 3: 11.3 ms per loop
In [22]: %timeit df[g.cumcount() == 1]
1000 loops, best of 3: 286 µs per loop