12

I currently have dataframe at the top. Is there a way to use a groupby function to get another dataframe to group the data and concatenate the words into the format like further below using python pandas?

Thanks

[enter image description [1]

Henry Ecker
  • 34,399
  • 18
  • 41
  • 57
user3655574
  • 692
  • 2
  • 9
  • 27
  • There is a similar question here: http://stackoverflow.com/questions/17841149/pandas-groupby-how-to-get-a-union-of-strings – johnchase Jun 30 '16 at 15:18

2 Answers2

31

You can apply join on your column after groupby:

df.groupby('index')['words'].apply(','.join)

Example:

In [326]:
df = pd.DataFrame({'id':['a','a','b','c','c'], 'words':['asd','rtr','s','rrtttt','dsfd']})
df

Out[326]:
  id   words
0  a     asd
1  a     rtr
2  b       s
3  c  rrtttt
4  c    dsfd

In [327]:
df.groupby('id')['words'].apply(','.join)

Out[327]:
id
a        asd,rtr
b              s
c    rrtttt,dsfd
Name: words, dtype: object
EdChum
  • 376,765
  • 198
  • 813
  • 562
11

If you want to save even more ink, you don't need to use .apply() since .agg() can take a function to apply to each group:

df.groupby('id')['words'].agg(','.join)

OR

# this way you can add multiple columns and different aggregates as needed.
df.groupby('id').agg({'words': ','.join})
ihightower
  • 3,093
  • 6
  • 34
  • 49
hamx0r
  • 4,081
  • 1
  • 33
  • 46