主席()
critic title rating
0 Jack Matthews Lady in the Water 3.0
1 Jack Matthews Snakes on a Plane 4.0
2 Jack Matthews You Me and Dupree 3.5
3 Jack Matthews Superman Returns 5.0
4 Jack Matthews The Night Listener 3.0
我想让它看起来像
critic Just My Luck Lady in the Water Snakes on a Plane Superman Returns The Night Listener You Me and Dupree
Claudia Puig 3.0 NaN 3.5 4.0 4.5 2.5
Gene Seymour 1.5 3.0 3.5 5.0 3.0 3.5
Jack Matthews NaN 3.0 4.0 5.0 3.0 3.5
Lisa Rose 3.0 2.5 3.5 3.5 3.0 2.5
Mick LaSalle 2.0 3.0 4.0 3.0 3.0 2.0
Toby NaN NaN 4.5 4.0 NaN 1.0
我试着使用
movie_rating= ratings.pivot(index='critic', columns='title',values='rating')
但它会创建额外的列名。像
title Just My Luck Lady in the Water Snakes on a Plane Superman Returns The Night Listener You Me and Dupree
critic
Claudia Puig 3.0 NaN 3.5 4.0 4.5 2.5
Gene Seymour 1.5 3.0 3.5 5.0 3.0 3.5
Jack Matthews NaN 3.0 4.0 5.0 3.0 3.5
Lisa Rose 3.0 2.5 3.5 3.5 3.0 2.5
Mick LaSalle 2.0 3.0 4.0 3.0 3.0 2.0
Toby NaN NaN 4.5 4.0 NaN 1.0
您看到的不是额外的列。这些是行和列的索引名称。行索引名为
'critic'
,列索引名为'title'
。要使数据透视框显示时不带它们,只需将name
参数设置为None
以表示index
和columns
您可以看到
title
和critic
分别是columns
和index
的名称属性要删除显示的名称,只需覆盖
index
和columns
对象的name
属性即可相关问题 更多 >
编程相关推荐