ulrich
ulrich

Reputation: 3587

Pandas: consolidating columns in DataFrame

Using the DataFrame below as an example:

import pandas as pd
df = pd.DataFrame({'col1':[1, 2, 3, 2, 1] , 'col2':['A', 'A', 'B', 'B','C']})

   col1 col2
0     1    A
1     2    A
2     3    B
3     2    B
4     1    C

how can I get

   col1  col2  
0   1     A,C  
1   2     A,B
2   3     B  

Upvotes: 1

Views: 82

Answers (1)

EdChum
EdChum

Reputation: 394409

You can groupby on 'col1' and then apply a lambda that joins the values:

In [88]:
df = pd.DataFrame({'col1':[1, 2, 3, 2, 1] , 'col2':['A', 'A', 'B', 'B','C']})
df.groupby('col1')['col2'].apply(lambda x: ','.join(x)).reset_index()

Out[88]:
   col1 col2
0     1  A,C
1     2  A,B
2     3    B

Upvotes: 3

Related Questions