timelfelt
timelfelt

Reputation: 680

Join or aggregate strings belonging to the same group in pandas

Given a CSV like this, how can I combine information from the same column?

First,Last,Email,Group
Tim,Elfelt,[email protected],Information Systems
Tim,Elfelt,[email protected],Technology Training

Should output this, based on combining the email column

First,Last,Email,Group
Tim,Elfelt,[email protected],Information Systems;Technology Training

Edit: thanks to coldspeed, working solution here:

import pandas as pd
data = pd.read_csv('combinedemails.csv', encoding='utf-8',
                   usecols=['First', 'Last', 'Email', 'Group', 'List']).groupby(['First', 'Last', 'Email']).Group.apply(
    '; '.join).reset_index(name='Group')

data.to_csv('output.csv', sep=',', encoding='utf-8')

Upvotes: 1

Views: 106

Answers (1)

cs95
cs95

Reputation: 402814

You can use groupby + str.join:

df.groupby(['First', 'Last', 'Email']).Group.apply('; '.join).reset_index(name='Group')

  First    Last           Email                                     Group
0   Tim  Elfelt  [email protected]  Information Systems; Technology Training

Upvotes: 5

Related Questions