I have a dataframe like below. I want to convert it to pivot table format, where there is each row for unique ID, new column for each Score with Type prefix.
I have about 15 different Types in the actual dataframe.
df = pd.DataFrame({'ID' : [1,1,2,2,3,3,4,4],
'Type':['A','B','A','B','A','B','A','B'],
'Score':[0.3,np.nan, 0.2, 0.1, 1.1,np.nan, 2, np.nan]})
Desired output
ID | A_Score | B_Score |
---|---|---|
1 | 0.3 | |
2 | 0.2 | 0.1 |
3 | 1.1 | |
4 | 2 |
I tried below and it almost does what I need but I need the column renames and need it in pandas dataframe
df2 = df.pivot_table(index=['ID'], columns='Type')