1

I have two rows that look like

CustomerID, CustomerName, ValA, ValB, ValC, Vald
1899, CustomerBlue, 2, 5, 8, 9
1899B, CustomerBlue, 3, 6, 7, 8

I want to combine the rows to become;

1899, CustomerBlue, 5, 11, 15, 17

I started with this;

df.groupby(['CustomerName'], as_index=False).agg('sum') 

But obviously that won't work because two columns are strings. Is there a way to replace the first two cells with what I want, and sum the rest?

  • Does this answer your question? [python pandas: applying different aggregate functions to different columns](https://stackoverflow.com/questions/32374620/python-pandas-applying-different-aggregate-functions-to-different-columns) – ljmc Jan 06 '23 at 21:04

1 Answers1

0

You can use groupby_agg with a dict mapping. Unfortunately, there is no default function to apply. You have to enumerate all columns...

>>> (df.groupby('CustomerName', as_index=False)
       .agg({'CustomerID': 'first', 'ValA': 'sum', 'ValB': 'sum',
             'ValC': 'sum', 'Vald': 'sum'}))

   CustomerName CustomerID  ValA  ValB  ValC  Vald
0  CustomerBlue       1899     5    11    15    17
Corralien
  • 109,409
  • 8
  • 28
  • 52