Reputation: 25
I have this dataframe:
identifier_1 measure Value identifier_2
abc height 12 oii
abc weight 122 oii
abc eye_color green Oii
cde height 43 lkj
cde weight 123 lkj
cde eye_color blue lkj
fgh height 32 mnb
fgh weight 125 mnb
fgh eye_color black mnb
I want to transform the column "measure" into a table header, and the corresponding cell in column value, to be its value, like:
identifier_1 height weight eye_color identifier_2
abc 12 122 green oii
cde 43 123 blue lkj
fgh 32 125 black mnb
I tried the pivot function in pandas but couldn't reach the required result.
Upvotes: 2
Views: 326
Reputation: 863236
df1 = (df.set_index(['identifier_1','identifier_2','measure'])['Value']
.unstack()
.reset_index()
.rename_axis(None, axis=1))
print (df1)
identifier_1 identifier_2 eye_color height weight
0 abc oii green 12 122
1 cde lkj blue 43 123
2 fgh mnb black 32 125
Upvotes: 1