熊猫根据列将两行合并为一行

时间:2018-08-03 13:38:21

标签: python python-2.7 pandas pandas-groupby pandasql

我有2行看起来像这样,

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

我希望将它们合并为一个:

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

不确定如何在Pandas中完成此操作。任何指针将不胜感激!预先感谢

1 个答案:

答案 0 :(得分:6)

IIUC

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