Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas groupby apply on one column and keeping the other columns [duplicate]

Tags:

python

pandas

I have a dummy df :

    name    value1    otherstuff1 otherstuff2 
0   Jack       1          1.19        2.39     
1   Jack       3          1.19        2.39
2   Luke       2          1.08        1.08  
3   Mark       4          3.45        3.45
4   Luke       1          1.08        1.08

I want to apply a complex function (not pertinent to explicit) to the value column over the grouped dataframe on 'name'. But I still want to keep track of the otherstuff column knowing that for each name, the 'otherstuffX' columns have the same value.

Expected output :

    name    value1    otherstuff1 otherstuff2 
0   Jack    184.2         1.19        2.39    
1   Luke    220.84        1.08        1.08  
2   Mark    0.58          3.45        3.45

Sample code that is working but makes otherstuff columns disappear :

df.groupby('name', sort=False)['value1'].apply(complex_function).reset_index()

What does work but I don't want to do :
Doing the above line then merging the other columns to the grouped dataframe.

Thanks

like image 814
Mrofsnart Avatar asked Dec 27 '25 21:12

Mrofsnart


1 Answers

There is groupby().agg:

df.groupby('name').agg({
   'value1': complex_function, 
   'otherstuff1': 'first', 
   'otherstuff2':'first'
})
like image 61
Quang Hoang Avatar answered Dec 30 '25 11:12

Quang Hoang



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!