1

I am looking for a way to copy the result of an aggregate call to each row in a dataframe.

I am currently doing it like this:

tmp = df.groupby('somekey').col_to_sum.sum().reset_index()
df = df.merge(tmp, how='left', on='somekey')

This gives me the result I want: tmp is a series of the sum for each value of somekey and the merge then "copies it" to each row in the original df (so each row contains the sum value).

Is there a shorter way to achieve the same result?

Laurent S
  • 4,106
  • 3
  • 26
  • 50
  • 1
    Use `transform`.... `df['newcol'] =df.groupby('somekey').col_to_sum.transform('sum')` – Scott Boston Jan 15 '19 at 16:01
  • Thanks. The duplicate above is also itself a duplicate of https://stackoverflow.com/questions/30949020/best-way-to-add-group-totals-to-a-dataframe-in-pandas :) It's interesting how each question is worded in completely different ways... – Laurent S Jan 16 '19 at 17:33
  • Yes... does this help? If not, you can edit the question a bit, and we'll try to help. – Scott Boston Jan 16 '19 at 17:38
  • 1
    Yes, it's exactly the solution I was looking for, thanks for answering! – Laurent S Jan 16 '19 at 17:41

0 Answers0