How to find subgroups statistics in pandas?

Posted by user2808117 on Stack Overflow See other posts from Stack Overflow or by user2808117
Published on 2013-10-25T03:36:02Z Indexed on 2013/10/25 3:54 UTC
Read the original article Hit count: 267

Filed under:

I am grouping a DataFrame using multiple columns (e.g., columns A, B -> my_df.groupby(['A','B']) ), is there a better (less lines of code, faster) way of finding how many rows are in each subgroup and how many subgroups are there in total? at the moment I am using:

def get_grp_size(grp):
    grp['size'] = len(grp)
    return grp
my_df = my_df.groupby(['A','B']).apply(get_grp_size)
my_df[['A','B','size']].drop_duplicates().size

© Stack Overflow or respective owner

Related posts about pandas