How to find subgroups statistics in pandas?
- by user2808117
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