-1

I am sure there is an easy way to do this but pivoting is my weak suite and I cannot figure it out. I provided code for the how the data is and what I want to transform it into

import pandas as pd

start_table = pd.DataFrame(
    {
        'ID' : [1,1,1,2,2,3],
        'LOCATION' : ['US', 'CANADA', 'MEXICO', 'US', 'MEXICO', 'US'],
        'STATUS' : ['IN PROGRESS', 'COMPLETE']*3
    }
)

end_table = pd.DataFrame(
    {
        'ID' : [1,2,3],
        'US' : ['IN PROGRESS', 'COMPLETE', 'COMPLETE'],
        'CANADA' : ['COMPLETE', None, None],
        'MEXICO' : ['IN PROGRESS', 'IN PROGRESS', None]
    }
)
trey hannam
  • 175
  • 2
  • 9
  • 1
    Does this answer your question? [How can I pivot a dataframe?](https://stackoverflow.com/questions/47152691/how-can-i-pivot-a-dataframe) - `start_table.pivot(index='ID', columns='LOCATION', values='STATUS')` – It_is_Chris Jul 11 '23 at 20:18

1 Answers1

-1

Chat GPT solved it by doing

end_table = start_table.pivot_table(index='ID', columns='LOCATION', values='STATUS', aggfunc='first').reset_index()
trey hannam
  • 175
  • 2
  • 9