How to merge rows with same index on a single data frame?

Question:

I have a dataframe that looks like this:

A          B           C
1          1234        Win
1          2345        Win
2          1987        Loss
3          3456        Win
3          4567        Win

And I want this to become:

A          B           C
1          1234,2345   Win
2          1987        Loss
3          3456,4567   Win

Note: C values always have the same value for the same index.

Anyone can help? Thanks!

Asked By: ChiefsCreation

||

Answers:

You can groupby on ‘A’ and ‘C’ seeing as their relationship is the same, cast the ‘B’ column to str and join with a comma:

In [23]:
df.groupby(['A','C'])['B'].apply(lambda x: ','.join(x.astype(str))).reset_index()

Out[23]:
   A     C          B
0  1   Win  1234,2345
1  2  Loss       1987
2  3   Win  3456,4567
Answered By: EdChum

You can use agg function.

In[3]:
df.groupby(['A']).agg(lambda col: ','.join(col))

Out[3]:
A   B           C
1   1234,2345   Win,Win   
2   1987        Loss   
3   3456,4567   Win,Win
Answered By: Y Z
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.