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')