Pandas: Wideset Dataframe Based On Condition
I would like to wideset the data frame every time there's a duplicate matching time + image column. Minimal Reproducible Example: This dataframe: Time Image Yo
Solution 1:
Use DataFrame.set_index
with GroupBy.cumcount
for counter of repeated columns, reshape by DataFrame.unstack
, sorting index by DataFrame.sort_index
, flatten MultiIndex
and last convert MultiIndex
to columns:
df = (df.set_index(['Time','Image', df.groupby(['Time','Image']).cumcount().add(1)])
.unstack()
.sort_index(level=1, axis=1, sort_remaining=False))
df.columns = df.columns.map(lambda x: f'{x[0]}{x[1]}')
df = df.reset_index()
print (df)
Time Image YorN1 Result1 Name1 Value1 YorN2 Result2 \
0 2020-11-21 13:40:56 W402 Y ACCEPTED David 2.11 NaN NaN
1 2020-11-21 13:41:03 W403 Y ACCEPTED David 1.04 NaN NaN
2 2020-11-21 13:45:16 W404 Y REJECTED David 18.31 N ACCEPTED
3 2020-11-21 14:01:01 W405 Y ACCEPTED Harry 1.41 NaN NaN
4 2020-11-21 14:01:07 NaN NaN NaN NaN NaN NaN NaN
Name2 Value2
0 NaN NaN
1 NaN NaN
2 Super 80.69
3 NaN NaN
4 NaN NaN
Post a Comment for "Pandas: Wideset Dataframe Based On Condition"