Explore Courses Blog Tutorials Interview Questions
0 votes
in Data Science by (50.2k points)

I want to merge several strings in a dataframe based on a groupedby in Pandas.

This is my code so far:

import pandas as pd

from io import StringIO

data = StringIO("""










# load string as stream into dataframe

df = pd.read_csv(data,header=0, names=["name","text","date"],parse_dates=[2])

# add a column with the month

df["month"] = df["date"].apply(lambda x: x.month)

I want the end result to look like this:


I don't get how I can use groupby and apply some sort of concatenation of the strings in the column "text". Any help appreciated!

1 Answer

0 votes
by (108k points)

You can just call apply function and then reset_index, refer the following code:




    name  month         text

0  name1     11   du

1  name1     12 aj,oj

2  name2     11 fin,katt

3  name2     12 mycket,lite

If you are interested in learning Pandas and want to become an expert in Python Programming, then check out this Python Course and upskill yourself.

Browse Categories