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
There is groupby().agg:
df.groupby('name').agg({
'value1': complex_function,
'otherstuff1': 'first',
'otherstuff2':'first'
})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With