Machine_leaning_9
Machine_leaning_9

Reputation: 47

Merge rows of pandas data frame by grouping a column

A beginner in panda and trying to achieve this in a pandas data frame-

Input- Input

I want an output like-

Output

It is groupby on name columns, the merge all the rows in Address and in same row store the total count of address.

Any help is appreciated.

thanks

Upvotes: 1

Views: 746

Answers (1)

Scott Boston
Scott Boston

Reputation: 153520

Use:

(df.groupby('Name', as_index=False)
   .agg({'Count':'sum','Address':lambda x: ' \n '.join(x)}))

Output:

   Name                              Address  Count
0  Jake  XXXXXXXXX \n YYYYYYYYY \n ZZZZZZZZZ      3
1   Jon                  AAAAAAA \n BBBBBBBB      2

Upvotes: 2

Related Questions