Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas Dataframe converting to pivot table [duplicate]

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')

enter image description here

like image 585
Daven1 Avatar asked Dec 07 '25 10:12

Daven1


1 Answers

You can do

out = df.pivot_table(index='ID', columns='Type',values='Score').add_prefix('Score_').reset_index()
Out[355]: 
Type  ID  Score_A  Score_B
0      1      0.3      NaN
1      2      0.2      0.1
2      3      1.1      NaN
3      4      2.0      NaN
like image 64
BENY Avatar answered Dec 09 '25 00:12

BENY