Compute unique groups from Pandas group-by results -
i'd count unique groups result of pandas group-by operation. instance here example data frame.
in [98]: df = pd.dataframe({'a': [1,2,3,1,2,3], 'b': [10,10,11,10,10,15]}) in [99]: df.groupby('a').groups out[99]: {1: [0, 3], 2: [1, 4], 3: [2, 5]} the conceptual groups {1: [10, 10], 2: [10, 10], 3: [11, 15]} index locations in groups above substituded values column b, first problem i've run how convert positions (e.g. [0, 3]) values b column.
given ability convert groups value groups column b can compute unique groups hand, secondary question here if pandas has built-in routine this, haven't seen.
edit updated target output:
this output looking in simplest case:
{1: [10, 10], 2: [10, 10], 3: [11, 15]}
and counting unique groups produce equivalent to:
{[10, 10]: 2, [11, 15]: 1}
how about:
>>> df = pd.dataframe({'a': [1,2,3,1,2,3], 'b': [10,10,11,10,10,15]}) >>> df.groupby("a")["b"].apply(tuple).value_counts() (10, 10) 2 (11, 15) 1 dtype: int64 or maybe
>>> df.groupby("a")["b"].apply(lambda x: tuple(sorted(x))).value_counts() (10, 10) 2 (11, 15) 1 dtype: int64 if don't care order within group.
you can trivially call .to_dict() if you'd like, e.g.
>>> df.groupby("a")["b"].apply(tuple).value_counts().to_dict() {(11, 15): 1, (10, 10): 2}
Comments
Post a Comment