Pandas: Wideset dataframe based on condition

  pandas, python

I would like to wideset the data frame every time the there’s a duplicate matching time + image column.

Minimal Reproducible Example:

This dataframe:

    Time                Image   YorN    Result      Name    Value
    2020-11-21 13:40:56 W402    Y       ACCEPTED    David   2.11
    2020-11-21 13:41:03 W403    Y       ACCEPTED    David   1.04
    2020-11-21 13:45:16 W404    Y       REJECTED    David   18.31
    2020-11-21 13:45:16 W404    N       ACCEPTED    Super   80.69
    2020-11-21 14:01:01 W405    Y       ACCEPTED    Harry   1.41
    2020-11-21 14:01:07 NaN     nan     NaN         NaN     NaN

Row 4 and 5 both have the same time and image id and hence this row would be widesetted.

Desired dataframe:

    Time                Image   YorN    Result      Name    Value   Result2  Name2  Value2
    2020-11-21 13:40:56 W402    Y       ACCEPTED    David   2.11    NaN      NaN    NaN
    2020-11-21 13:41:03 W403    Y       ACCEPTED    David   1.04    NaN      NaN    NaN
    2020-11-21 13:45:16 W404    Y       REJECTED    David   18.31   ACCEPTED Super  80.69
    2020-11-21 14:01:01 W405    Y       ACCEPTED    Harry   1.41    NaN      NaN    NaN
    2020-11-21 14:01:07 NaN     nan     NaN         NaN     NaN     NaN      NaN    NaN

Source: Python Questions

LEAVE A COMMENT