Search code examples
pythonpandasdataframemergeconcatenation

Merge/concatenate rows containing duplicate ids and TRUE/FALSE values, retaining only TRUE


Given a dataframe

{'id': {0: 100, 1: 100, 2: 101, 3: 101, 4: 102},
 'A': {0: True, 1: False, 2: True, 3: False, 4: True},
 'B': {0: False, 1: True, 2: False, 3: True, 4: False},
 'C': {0: False, 1: False, 2: False, 3: False, 4: False}}

which looks like this

enter image description here

How do we merge rows so that there is only one row for each unique id, retaining all True values?

Goal:

enter image description here


Solution

  • Use groupby.max:

    df.groupby('id', as_index=False).max()