0

I have a dataset like:

df = pd.DataFrame({'Topic': ['A', 'A', 'B', 'B'], 'Value': [3,5,2,9]})

But I want to add 'Sub_total' column aggregate by Topic:

Topic  Value  Sub_total
A      3      8
A      5      8
B      2      11
B      9      11

I have to use groupby and join back with the orignal df. Is there any faster way? Thanks in advance!

df.groupby('Topic')['Value'].sum().merge(df, on='Topic') 
eyllanesc
  • 221,139
  • 17
  • 121
  • 189
Homesand
  • 393
  • 2
  • 13

1 Answers1

0

Use groupby and transform:

df['Sub_total'] = df.groupby('Topic').transform('sum')
Code Different
  • 82,550
  • 14
  • 135
  • 153