CodeSsscala
CodeSsscala

Reputation: 749

Pandas Merge two rows into a single row based on columns

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

Upvotes: 9

Views: 12765

Answers (1)

BENY
BENY

Reputation: 323236

IIUC

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

Upvotes: 8

Related Questions