To concatenate strings from several rows using Python Pandas groupby, we can use the transform
method.
For instance, we write
df['text'] = df[['name','text','month']].groupby(['name','month'])['text'].transform(lambda x: ','.join(x))
df[['name','text','month']].drop_duplicates()
to create the text
column that calls groupby
on the selected columns name
and month
.
And then we get the text
column from the grouped data frame and call transform
with a lamnda function to join the strings together.
And then we call drop_duplicates
to drop the duplicate rows.