简体   繁体   中英

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  

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM