Python Pandas: Groupby Sum AND Concatenate Strings

You can also just tell .agg() which aggregator functions to use for each column, and for the string columns, pass ' '.join (notice there're no parenthesis since you don't want to call .join but rather pass it as the argument itself):

df.groupby(['ID','Name'],as_index=False).agg({'COMMENT1': ' '.join, 'COMMENT2': ' '.join, 'NUM': 'sum'})


Let us make it into one line

df.groupby(['ID','Name'],as_index=False).agg(lambda x : x.sum() if x.dtype=='float64' else ' '.join(x))
Out[1510]: 
   ID Name  COMMENT1      COMMENT2  NUM
0   1  dan    hi you  hello friend  3.0
1   2  jon       dog           cat  0.5
2   3  jon  yeah yes       nope no  3.1

Tags:

Python

Pandas