pandas - Merge nearly duplicate rows based on column value

后端 未结 3 1214
小鲜肉
小鲜肉 2020-11-27 11:34

I have a pandas dataframe with several rows that are near duplicates of each other, except for one value. My goal is to merge or \"coalesce\" these rows into a

3条回答
  •  北海茫月
    2020-11-27 12:12

    You can groupby and apply the list function:

    >>> df['Use_Case'].groupby([df.Name, df.Sid, df.Revenue]).apply(list).reset_index()
        Name    Sid     Revenue     0
    0   A   xx01    $10.00  [Voice, SMS]
    1   B   xx02    $5.00   [Voice]
    2   C   xx03    $15.00  [Voice, SMS, Video]
    

    (In case you are concerned about duplicates, use set instead of list.)

提交回复
热议问题