Using groupby on already grouped data in Pandas

Question:

I would like to achieve the below in Python using Pandas.

Enter image description here

I tried groupby and sum on the id and Group columns using the below:

df.groupby(['id','Group'])['Total'].sum()

I got the first two columns, but I’m not sure on how to get the third column (Overall_Total).

How can I do it?

Initial data (before grouping)

Enter image description here

Asked By: Alucor Rpa

||

Answers:

Assuming df is your initial dataframe, please try this:

df_group = df.groupby(['id','group']).sum(['time']).rename(columns={'time':'Total'})
df_group['All_total'] = df_group.groupby(['id'])['Total'].transform('sum')
Answered By: prahasanam_boi
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.