I have dataframe like this
ID Product Amount
22 product1 $10
22 product2 $20
22 product3 $30
33 product2 $4
33 product3 $5
44 product1 $78
44 product4 $90
How can i transpose this to below output (Product column values becomes new columns)
ID product1 product2 product3 product4
22 $10 $20 $30
33 $4 $5
44 $78 $90
Here you go, use pivot
test_df = pd.read_csv(StringIO("""ID Product Amount
22 product1 $10
22 product2 $20
22 product3 $30
33 product2 $4
33 product3 $5
44 product1 $78"""),sep='\s+')
test_df.pivot(index="ID",columns='Product',values='Amount')
Product product1 product2 product3
ID
22 $10 $20 $30
33 NaN $4 $5
44 $78 NaN NaN
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.