Search code examples
pythonpandasgroup-byaggregate

How to access group keys during aggregation in pandas groupby?


Is there a way to access the group keys from inside the aggregation functions? For example we have the following dataframe:

>>> df = pd.DataFrame({
        'score' : [2,2,3,3,3],
        'age' : [17,23,18,12,15]
    })
>>> df
   score  age
0      2   17
1      2   23
2      3   18
3      3   12
4      3   15

And do something like

df.groupby('score').agg(
    score_age = ('age', lambda x: x.groupkey + sum(x))
)

to get

       score_age
score           
2             42
3             48

Obviously x.groupkey does not work. What's the right syntax to access it? I can't seem to find it anywhere. Please help.


Solution

  • Use:

    df1 = df.groupby('score').agg(score_age = ('age', lambda x: x.name + x.sum()))
    print (df1)
           score_age
    score           
    2             42
    3             48