1

I have the following dataset:

ID Name Stages Date
1 John Interview 02/01/2022
2 Mario Apply 01/01/2022
3 Luis Interview 02/01/2022
2 Mario Interview 02/01/2022
2 Mario Offer 03/01/2022
3 Luis Offer 03/01/2022

and I want to make it look like this:

ID Name Apply Interview Offer
1 John NaN 02/01/2022 NaN
2 Mario 01/01/2022 02/01/2022 03/01/2022
3 Luis NaN 02/01/2022 03/01/2022
df = df.reset_index()
df.pivot( index = ['index','Name'],columns = ['Stages'], values = 'Dates')

This shows the dates by stage, but it doesn't group by Name and I've been stuck trying other stuff.

2 Answers2

1

reset_index after the pivot, not before, and use ID not index as new index:

(df.pivot(index=['ID','Name'], columns='Stages', values='Date')
   .reset_index()
   .rename_axis(columns=None)
)

output:

   ID   Name       Apply   Interview       Offer
0   1   John         NaN  02/01/2022         NaN
1   2  Mario  01/01/2022  02/01/2022  03/01/2022
2   3   Luis         NaN  02/01/2022  03/01/2022
mozway
  • 194,879
  • 13
  • 39
  • 75
0

You can remove the 'index' part of your code to get desired results

df = df.pivot( index = ['Name'],columns = ['Stages'], values = 'Date')
df.reset_index(inplace = True)
df[['Name', 'Apply', 'Interview', 'Offer']]
ArchAngelPwn
  • 2,891
  • 1
  • 4
  • 17