Search code examples
pythonpandasdataframegroup-bysum

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

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.