pandas data frame concatenate as aggregrate function for group by

Viewed 5

I have the following data in my data-frame

ID currency cost
1 USD 5
1 CNY 3
2 USD 1
2 CNY 1

The group by of cost is easy by id. But the currency column I want to concatenate in the final result.

ID currency cost
1 USD,CNY 8
2 USD,CNY 2

Is it possible to pass a custom function to the aggregate column that does the concatenate, like shown below?

df = df.groupby(['id']).agg(
    currency=('runId', **my-custom-concatenate-function**),
    revenue=('cost', 'sum')
).reset_index()
0 Answers
Related