0

I have the following dataframe:

df = pd.DataFrame({'group':['A','A','A','B','B','B'],'value':[1,2,3,4,5,6]})

I would like to add a column that will sum all the values for each group. This way each row with group A will have 6 in that column and each row with group B will have 15:

dfPartition = pd.DataFrame({"group": ['A','A','A','B','B','B'], "value":[1,2,3,4,5,6], "GroupSum": [6,6,6,15,15,15]})

In SQL I would do:

SUM(value) OVER (PARTITION BY group)

How would I do it in Pandas?

This is not group by! I need to leave the dataframe as is and add an additional column, with the groupby results

Hana
  • 49
  • 5

0 Answers0