0
$\begingroup$

My Question is about pandas DataFrame, I have two DataFrame both follow the same structure.

df_1:

Index    Text     Category
0        Text 01        1
1        Text 02        1
2        Text 03        1
3        Text 04        1

df_2:

Index    Text     Category
0        Text 05        2
1        Text 02        2
2        Text 09        2
3        Text 04        2

I want to marge both for this purpose I use pd.concat(df_1, df_2) but it simply merge both files, but I want to merge in that manner.

df_merge:

Index    Text     Category
0        Text 01         1
1        Text 02       1,2
2        Text 03         1
3        Text 04       1,2
4        Text 05         2
5        Text 09         2

But I really don't know how to do that.

$\endgroup$
1
$\begingroup$

Try:

Minimum reproducible example:

 A = pd.DataFrame({"Text":["01", "02","03","04"], "Category":[1,1,1,1]})
 B = pd.DataFrame({"Text":["05", "02","09","04"], "Category":[2,2,2,2]})

 C = pd.concat([A,B], ignore_index= True)

 C.groupby("Text").Category.unique().to_frame()

Result:

enter image description here

$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.