Q:

Remove nth row in groupby

I want to remove the nth row of a groupby object, say the last row.  I can extract this row using groupby.nth
Is there a similar method to remove the nth row, or equivalently get all the rows except the nth row? 

A:

You can find index of all nth rows and then select Index.difference by ix:
import pandas as pd

df = pd. DataFrame({'A':[1,1,1,2,2,2],
                   'B':[4,5,6,7,8,9]})

print (df)
   A  B
0  1  4
1  1  5
2  1  6
3  2  7
4  2  8
5  2  9

print (df.ix[df.index.difference(df.groupby('A', as_index=False)['B'].nth(1).index)])
   A  B
0  1  4
2  1  6
3  2  7
5  2  9

idx = df.groupby('A', as_index=False)['B'].nth(1).index
print (idx)
Int64Index([1, 4], dtype='int64')

print (df.index.difference(idx))
Int64Index([0, 2, 3, 5], dtype='int64')

print (df.ix[df.index.difference(idx)])
   A  B
0  1  4
2  1  6
3  2  7
5  2  9

If need all rows without last, use GroupBy.tail:
print (df.ix[df.index.difference(df.groupby('A')['B'].tail(1).index)])

   A  B
0  1  4
1  1  5
3  2  7
4  2  8

Timings:
In [27]: %timeit (df.groupby('A').apply(lambda x: x.iloc[:-1, :]).reset_index(0, drop=True).sort_index())
100 loops, best of 3: 2.48 ms per loop

In [28]: %timeit (df.ix[df.index.difference(df.groupby('A')['B'].tail(1).index)])
1000 loops, best of 3: 1.29 ms per loop

In [29]: %timeit (df.ix[df.index.difference(df.groupby('A', as_index=False)['B'].nth(1).index)])
The slowest run took 4.42 times longer than the fastest.  This could mean that an intermediate result is being cached. 
1000 loops, best of 3: 1.48 ms per loop

