Pandas Merge two rows into a single row based on columns

Kshitij G picture Kshitij G · Aug 3, 2018 · Viewed 10.2k times · Source

I have 2 rows that look like these,

------------------------------
DealName | Target | Acquirer |
-----------------------------
ABC-XYZ  | ABC    | None     |
------------------------------
ABC-XYZ  | None   | XYZ      |
------------------------------

I'm looking to merge them into a single as:

------------------------------
DealName | Target | Acquirer |
-----------------------------
ABC-XYZ  | ABC    | XYZ      |
------------------------------

Not sure how to accomplish this in Pandas. Any pointers will be highly appreciated! Thanks in advance

Answer

BENY picture BENY · Aug 3, 2018

IIUC

df.replace('None','').groupby('DealName',as_index=False).agg(''.join)
Out[25]: 
  DealName Target Acquirer
0  ABC-XYZ    ABC      XYZ