'How to use groupby to concatenate strings in python pandas?

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]



Solution 1:[1]

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})

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 ihightower