简体   繁体   中英

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.

Use:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM