如何将一个数据帧转换为另一个数据帧?

时间:2017-10-28 02:59:24

标签: python dataframe

ratings.head()

    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

1 个答案:

答案 0 :(得分:1)

您看到的不是额外的列。这些是行和列的索引的名称。行索引名为'critic',列索引名为'title'。要在没有它们的情况下显示透视数据框,您只需为nameNone设置index参数为columns

import pandas as pd

df = pd.DataFrame({'critic':list('AABBCC'), 
                   'title':['hello','world']*3, 
                   'rating':[4,3,6,2,3,2]}

p = df.pivot(index='critic', columns='title',values='rating')
p
# returns:
title   hello  world
critic
A           4      3
B           6      2
C           3      2

您可以看到titlecritic分别是columnsindex的名称属性

p.columns, p.index
# returns:
(Index(['hello', 'world'], dtype='object', name='title'),
 Index(['A', 'B', 'C'], dtype='object', name='critic'))

要删除显示的名称,您只需覆盖nameindex个对象的columns属性。

p.index.name = None
p.columns.name = None
p
# returns:
   hello  world
A      4      3
B      6      2
C      3      2