table pivot with pandas

Viewed 29

I would like to convert the table

Time Params Values Lot ID
t1 A 3 a1
t1 B 4 a1
t1 C 7 a1
t1 D 2 a1
t2 A 2 a1
t2 B 5 a1
t2 C 9 a1
t2 D 3 a1
t3 A 2 a2
t3 B 5 a2
t3 C 9 a2
t3 D 3 a2

to

Time A B C D Lot ID
t1 3 4 7 2 a1
t2 2 5 9 3 a1
t3 3 4 7 2 a2

Tried

df.pivot(index = 'Time', columns = 'Params', values = 'Values')

but it didn't come out with Lot ID

How can I add that to the table as an additional column?

1 Answers

The index argument of pivot can take a list.

df.pivot(index = ['Time', 'Lot ID'], columns = 'Params', values = 'Values')

Will work for you.

Related