I have a dataframe in the following format that I am trying to reshape into wide format in pandas.
But I am getting the error Index contains duplicate entries, cannot reshape.
df:
| id | status | value |
|---|---|---|
| 1 | item_ordered | complete |
| 1 | item_received | complete |
| 1 | item_setup | complete |
| 2 | item_ordered | complete |
| 2 | item_setup | complete |
| 2 | item_setup |
The format that I am trying to reshape to:
| id | item_ordered | item_received | item_setup |
|---|---|---|---|
| 1 | complete | complete | complete |
| 2 | complete | complete |
>Solution :
Group the dataframe by id and status columns, then take the first values for value column, finally unstack the resulting series:
>>> df.groupby(['id', 'status']).value.first().unstack().reset_index()
status id item_ordered item_received item_setup
0 1 complete complete complete
1 2 complete NaN complete