Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to aggregate the dataframe into one using python?

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading