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

Pandas dataframe – How to make a sum by using group by?

I have the following dataset and I want to sum the values of the column UnitPrice grouping by CustomerID.

enter image description here

I’m trying the following way but despite the new column is being added the values are not being filled

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

data['TotalEN'] = round(data.groupby(['SalesOrderID'])['UnitPrice'].sum(),2)

I tried to print the function if is calculating the values correctly and indeed it is

print(data.groupby(['CustomerID'])['UnitPrice'].sum())

enter image description here

What I’m doing wrong?

>Solution :

In this case, the shape of the output from the groupby operation will be different than the shape of your dataframe. You will need to use the transform method on the groupby object to restore the correct shape you need:

data['TotalEN'] = data.groupby(['SalesOrderID'])['UnitPrice'].transform('sum').round(2)

You can read more about transform here.

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