Python - Pandas — How to check which column a DataFrame is grouped by? -


imagine have function outputs grouped dataframe. want find out column dataframe grouped. how can this?

edit:

here's code:

from pandas import dataframe df = dataframe({'a' : [0, 1, 2],                 'b' : [1, 6, 5],                 'c' : [2, 5, 4]                  } ) grp = df.groupby('a') 

the question how determine grp grouped a.


below, john-galt gives extremely helpful answer. however, i've found 1 case it's not how apply solution: using custom grouping function.

edit 2: never mind, case not thought was. question has been answered.

you use grp.grouper.names

like, datframe

in [47]: df out[47]:     b  c 0  0  1  2 1  1  6  5 

grp grouped object

in [48]: grp = df.groupby('a') 

use grouper.names column names.

in [49]: grp.grouper.names out[49]: ['a'] 

also, grp.grouper object has lot of other useful metadata, may find useful

in [50]: grp.grouper. grp.grouper.agg_series       grp.grouper.group_info       grp.grouper.names grp.grouper.aggregate        grp.grouper.group_keys       grp.grouper.ngroups grp.grouper.apply            grp.grouper.groupings        grp.grouper.nkeys grp.grouper.axis             grp.grouper.groups           grp.grouper.result_index grp.grouper.compressed       grp.grouper.indices          grp.grouper.shape grp.grouper.get_group_levels grp.grouper.labels           grp.grouper.size grp.grouper.get_iterator     grp.grouper.levels           grp.grouper.sort 

Comments

Popular posts from this blog

jquery - How do you format the date used in the popover widget title of FullCalendar? -

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -