Unstack columns in pandas-python


I would like to Unpivot/unstack my dataframe. My df is like:


And I need it to be like:

Excel snapshot

I have tried with pd.melt and pd.unstack() but not with the desired results. Can you give me a hand? Thanks


You could use pivot_table()

a.pivot_table(index='Name', columns=['Title'], values='Status', aggfunc='first')


         Course1   Course2   Course3
Jhonny  Accepted       NaN  Rejected
Jim     Accepted       NaN       NaN
John    Finished       NaN       NaN
Mike         NaN  Accepted       NaN

I think you have a typo in your sample DF above. I think it should be:

a = pd.DataFrame(columns=['Name','Title','Status'], data=[['John','Course1','Finished'],['Mike','Course2','Accepted'],['Jim','Course1','Accepted'],['Jhonny','Course3','Rejected'],['Jhonny','Course1','Accepted']])

Answered By – sophocles

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published