简体   繁体   中英

Merge duplicated rows in pandas

Need to merge contents in the second row if find duplicates in the second row.

Input:

df=pd.DataFrame({'A':['A001','A002','A002','A003'],
                 'B':['ROW1','ROW2','ROW3','ROW4']})

Expected Output:

    A   B
0   A001    ROW1
1   A002    ROW2ROW3
2   A003    ROW4

groupby "A", agg using ''.join to join the strings:

df.groupby('A', as_index=False).agg(''.join)

output:

      A         B
0  A001      ROW1
1  A002  ROW2ROW3
2  A003      ROW4

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