Python Pandas: How to groupby and count and select a portion of counts?

I have a df like this:

          new_org               old_org    asn  cc
0    85736 pcizzi   85736 - Pcizzi S .a  23201  PY
1             001              001 Host  40244  US
2      85736 blah       85736 - whatevs  23201  PY
3             001        001 IT Complex  55734  IN
4  001 hospedagem   001 Hospedagem Ltda  36351  US
5          001web  36351  US

and I would like to groupby my df based on ‘asn’ column and select those groups which have more than one row. This is how I am doing it now but I am not sure if it is correct:

df.groupby('asn').apply(lambda x:x.count()>1)

Can anybody help?

Source: python

1 Comment

  1. Edwin

    You can filter a group.

    Try df.groupby(‘asn’).filter(lambda x: len(x) > 1) which will return you a DataFrame. You could group it again further if necessary.


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.