python - How to find the number of groups in multi-index groupby object in pandas? -


my question simple, not find answer anywhere looked.

i want have number of groups in multi-index pandas groupby object. note not same number of elements in groups (use .size()), nor overall number of groups (use len. see here).

it best illustrate example.

let's create simple dataframe:

import pandas pd df = pd.dataframe({'group': ['gr1','gr1','gr2','gr2','gr3','gr3'],                    'kind': ['sweet','sour','sweet','sour','sweet','sour'],                    'values': [10,11,200,201,300,301]}) 

now group using 2 columns:

gr = df.groupby(['group','kind']) 

this produces desired groupby object. has total of 6 groups, can verify with:

len(gr) 

i can iterate through groups:

for key,group in gr:     print key 

this produces following:

('gr1', 'sour') ('gr1', 'sweet') ('gr2', 'sour') ('gr2', 'sweet') ('gr3', 'sour') ('gr3', 'sweet') 

we can see first key has 3 unique entries, second 2 unique entries.

what looking given gr returns (3,2) without having access original dataset gr generated , without iterating through groupby object, building list, , finding unique elements.

the shortest way can think of might be

>>> gr.dtypes.index.levshape (3, 2) 

basically, need handle on groups in form of multiindex:

>>> gr.dtypes               group    kind values group kind                         gr1   sour   object  object  int64       sweet  object  object  int64 gr2   sour   object  object  int64       sweet  object  object  int64 gr3   sour   object  object  int64       sweet  object  object  int64 >>> gr.dtypes.index multiindex(levels=[['gr1', 'gr2', 'gr3'], ['sour', 'sweet']],            labels=[[0, 0, 1, 1, 2, 2], [0, 1, 0, 1, 0, 1]],            names=['group', 'kind']) >>> gr.dtypes.index.levels frozenlist([['gr1', 'gr2', 'gr3'], ['sour', 'sweet']]) >>> gr.dtypes.index.levshape (3, 2) 

originally thinking

>>> pd.series(gr.groups).index.levshape (3, 2) 

to manufacture new index groups dictionary, looks info there in dtypes.


Comments

Popular posts from this blog

routing - AngularJS State management ->load multiple states in one page -

python - GRASS parser() error -

Swift game error message -