UserYmY
UserYmY

Reputation: 8554

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  action.us.001web.net  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?

Upvotes: 1

Views: 148

Answers (1)

ComputerFellow
ComputerFellow

Reputation: 12108

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.

Upvotes: 4

Related Questions