有没有一种方法可以从单元格中创建新列,并使其值来自另一列



我正试图找到一种方法,从pandas DataFrame中的一列获取信息,并将其唯一值作为新列,将其分数作为新形成列中的值。即

分护>6
索引 产品 测试
0 A
1
2 B 保护
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

相关内容

最新更新