I checked around as much as I could find.
If I use groupby
in pandas, and I have a group, call it group1
, how do I get group1's name?
I am using groupby
and apply
, so I am not explicitly pulling the groups, which is why I need to do this.
Suppose of group df by two things.
df.groupby(['key1','key2'])
Then I get a group using this: http://pandas.pydata.org/pandas-docs/stable/generated/pandas.core.groupby.GroupBy.get_group.html#pandas.core.groupby.GroupBy.get_group
I want to avoid doing:
group1.key1.unique()[0]
group1.key2.unique()[0]
to get the name because that is slow..
It's not clear to me what you mean by name of the group. Do you mean the values in the column you are grouping by?
Apply will break the dataframe into multiple smaller dataframes by the groupby columns. The columns you group by are still inside the smaller dataframes. Is that what you are after?
As an illustration:
example data:
let's group it and make up a silly function:
so you can see in the resulting printed output that each iteration of the
apply
gets all columns of the input dataframe.EDIT:
I'm not sure how to grab a tuple of keys from an
apply
but I can from a loop: