我正试图找到一种方法,从pandas DataFrame中的一列获取信息,并将其唯一值作为新列,将其分数作为新形成列中的值。即
索引 | 产品 | 测试 | 分
---|---|---|
0 | A | 护|
1 | ||
2 | B | 保护 | >6
3 |
使用pivot()
:
df.pivot(index = 'Product', columns = 'Test', values = 'Score')
退货:
Product Comfort Protection
A 6 5
B 7 6
如果您希望有数字索引或将"产品"作为列而不是索引,请添加reset_index()
:
df.pivot(index = 'Product', columns = 'Test', values = 'Score').reset_index()
退货:
Product Comfort Protection
0 A 6 5
1 B 7 6