0

I have a large dataset that is similar in structure to this:

enter image description here

I am trying to get an ouput that groups and sums by customer and metal like this:

enter image description here

Is there a simple way to do this in Pandas without having to iterate over rows? This difference I have here between this and other groupby() sum() questions is that in this case I am not trying to sum a single column, but multiple columns.

William
  • 405
  • 6
  • 11
  • 26
  • 1
    Grouping and then taking the first value of account and the sum of all others should do this: `grouped = df.groupby(["Metal", "Customer"])` `pd.merge(grouped["Acct"].first(), grouped.sum(), left_index=True, right_index=True)` – Rawson May 19 '23 at 16:25
  • Once I got this suggestion working it seemed to completely drop the metal and customer columns after the merge. I did alter it to be ```df=pd.merge(grouped["Acct"].first(), grouped.sum(), left_index=True, right_index=True)``` – William May 19 '23 at 17:31
  • They are now the index. If you add in `df.reset_index(drop=False, inplace=True)` then they will become columns again. – Rawson May 19 '23 at 17:37

0 Answers0