Pyhon – Pandas, is it possibleto group rows where a column cell equals a specific value?

Question:

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?

Asked By: solidstatestarfish

||

Answers:

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
Answered By: Corralien
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.