df = pd.DataFrame(np.random.randint(10, size=(10 ** 2, 3)), columns=list('ABC')
    )
result = df.groupby(['A', 'B']).tail(2).groupby(['A', 'B']).head(1)
expected = df.groupby(['A', 'B'], as_index=False).apply(lambda x: x if len(
    x) == 1 else x.iloc[[-2]]).reset_index(level=0, drop=True)
assert expected.sort_index().equals(result)
Python Pandas Drop Duplicates keep second to last