Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 875
Loqz Avatar asked Nov 04 '25 10:11

Loqz


1 Answers

Use:

df1 = df.groupby('score').agg(score_age = ('age', lambda x: x.name + x.sum()))
print (df1)
       score_age
score           
2             42
3             48
like image 178
jezrael Avatar answered Nov 07 '25 05:11

jezrael



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!