How to aggregate the dataframe into one using python?

Question:

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

Asked By: Sujith George

||

Answers:

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()
Answered By: INGl0R1AM0R1
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.