Home > Blockchain >  Pyhon - Pandas, is it possibleto group rows where a column cell equals a specific value?
Pyhon - Pandas, is it possibleto group rows where a column cell equals a specific value?

Time:01-07

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?

CodePudding user response:

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
  • Related