Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

python pandas reshape data from long to wide

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading