2

I have a dataset which looks like this:

         location_id  ...  partner_code     product_code  value
24275            239  ...           AUT             8412      0
24496            239  ...           CHN             8412   1000
24742            239  ...           DEU             8412  20000
24831            239  ...           DNK             8412   4000
25094            239  ...           FRA             8412      0
...              ...  ...           ...              ...    ...
3490121          239  ...           KOR             2503   3000
3490152          239  ...           MYS             2503  15000
3490398          239  ...           CAN             2503      0
3490519          239  ...           IND             2503      0
3490597          239  ...           SGP             2503    350

I want to aggregate the values of the value column by the product_code values, so that for each product code, there is just one value that is a sum of values across all partners, which should look like this:

         location_id  ...  partner_code     product_code  value
n                239  ...           ALL             8412  25000
...              ...  ...           ...              ...    ...
n                239  ...           ALL             2503  18350

I tried using something like: filtered_data = data.groupby('product_code').agg('value'), which creates an uncallable groupby object.

How can I modify this code to perform the desired aggregation?

  • Does this answer your question? [Pandas group-by and sum](https://stackoverflow.com/questions/39922986/pandas-group-by-and-sum) – Let's try Aug 25 '20 at 16:56
  • After aggregation, all the data in the `partner_code` column become `ALL`? – Terry Aug 25 '20 at 17:12

1 Answers1

1

Not sure what you want to do with other columns, but this should solve your query

data.groupby('product_code')['value'].sum()
NiteshK
  • 101
  • 3