python 3.x - Sorting the grouped data as per group size in Pandas -
i have 2 columns in dataset, col1 , col2. want group data per col1 , sort data per size of each group. is, want display groups in ascending order of size.
i have written code grouping , displaying data follows:
grouped_data = df.groupby('col1') """code sorting comes here""" name,group in grouped_data: print (name) print (group)
before displaying data, need sort per group size, not able do.
you can use python's sorted:
in [11]: df = pd.dataframe([[1, 2], [1, 4], [5, 6]], index=['a', 'b', 'c'], columns=['a', 'b']) in [12]: g = df.groupby('a') in [13]: sorted(g, # iterates pairs of (key, corresponding subdataframe) key=lambda x: len(x[1]), # sort number of rows (len of subdataframe) reverse=true) # reverse sort i.e. largest first out[13]: [(1, b 1 2 b 1 4), (5, b c 5 6)]
note: iterator g
, iterates on pairs of key , corresponding subframe:
in [14]: list(g) # happens same above... out[14]: [(1, b 1 2 b 1 4, (5, b c 5 6)]
Comments
Post a Comment