Search code examples
pythonpandasdataframeconcatenationmultiple-columns

How can I combine the texts with same date in the python dataframe?


I have the following data frame and I want to gather texts with the same date on a single date.

date        text
2020-09-10  text1
2020-09-10  text2
2020-09-09  text3
2020-09-09  text4
2020-09-08  text5
2020-09-08  text6

My expected data frame is like that.

date        text
2020-09-10  text1text2
2020-09-09  text3text4
2020-09-08  text5text6

Solution

  • df.groupby('date').agg({'text':'sum'})

    date        text
    2020-09-10  text1text2
    2020-09-09  text3text4
    2020-09-08  text5text6