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
  • 235,170
  • 19
  • 170
  • 241
Homesand
  • 423
  • 4
  • 14

1 Answers1

0

Use groupby and transform:

df['Sub_total'] = df.groupby('Topic').transform('sum')
Code Different
  • 90,614
  • 16
  • 144
  • 163