DF1:
Product Party x y Currency Parent Child
Purchase 5AAA64870 2 0.1 USD Apple Smartphone
Purchase 5AAA64870 1 0.3 USD Apple Smartphone
Purchase 5AAA64870 1 0.3 USD Apple Smartphone
Purchase 5AAA64870 1 0.3 USD Apple Smartphone
Purchase 5AAA64870 1 0.3 USD Apple Smartphone
Purchase 5AAA64870 1 0.3 USD Apple Smartphone
Expected output:
Product Party x y Currency Parent Child
Purchase 5AAA64870 7 1.6 USD Apple Smartphone
Code:
df1 = df1[df1['Product'] == 'Purchase'].groupby(
['Product', 'Party'])['x', 'y'].sum()
Sorry, this sounds very easy question, but I am unable to aggregate the data into one row like the above expected output. Appreciate your help in this
CodePudding user response:
It was pretty easy :). But just because you are a newbee i am gonna explain the logic behind it, so when you groupby and pass a list. Pandas basically says okay this is now the index so if the index is equal another index i shall smush them together. If you add index columns, well he is gonna smush them together according to your elements.
df1[df1['Product'] == 'Purchase'].groupby(
['Product', 'Party','Parent','Currency','Child'],as_index=False)['x', 'y'].sum()