Good approach to get top n records within each group?

User 570 | 11/10/2014, 1:09:44 PM

Hi,

How to get top n records within each group?

thanks!

Comments

User 570 | 11/12/2014, 3:43:13 PM

with small spelling correction :)

sf = sf.sort(['groupcol', 'datacol']) sf = sf.addrownumber('id') grp = sf.groupby('groupcol', {'Minofid':gl.aggregate.MIN('id')}) sf = sf.join(grp, 'groupcol', how='left') sf['rank'] = sf['id'] - sf['Minofid'] sf = sf[sf['rank'] < 10]

Thanks!!!