I have a dataframe with fields as follow
| field_1 | field_2 | field_3 |
|---|---|---|
| A | 2 | 4 |
| B | 5 | 7 |
I would like aggregate field_2 i.e:
| field_1 | field_2 | field_3 | field_2_agg |
|---|---|---|---|
| A | 2 | 4 | 7 |
| B | 5 | 7 | 7 |
I usually create a new dataframe by doing a groupby on the dataframe with the field I want and then merge back the grouped dataframe to the original one. Is there an easier way to do this?
>Solution :
Try this:
df['field_2_agg'] = df['field_2'].sum()
Output:
>>> df
field_1 field_2 field_3 field_2_agg
0 A 2 4 7
1 B 5 7 7