How to merge rows with same index on a single data

2020-08-13 07:44发布

问题:

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!

回答1:

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


标签: python pandas