您的位置:首页 > 编程语言 > Python开发

python数据分析chapter2-2

2017-06-21 20:29 465 查看

1 利用pandas对电影评分数据进行分析

数据来源于20世纪90年代末到21世纪初由Movielens用户提供的电影评分数据。这些数据包括电影评分、电影原数据(风格类型和年代)以及关于用户的人口统计学数据(年龄、邮编、性别和职业等)。数据集含有来自6000名用户对4000部电影的100万条评分数据。他分为三个表:评分、用户信息和电影信息。

1.1 下载并展示原始数据

import pandas as pd

#读取用户数据表,并指定列名
userColumnsNames = ['user_id','gender','age','occupation','zip']
user = pd.read_table('E:\python\pythonDataAnalysis\pydata-book-master\ch02\movielens\users.dat',sep='::',header=None,names=userColumnsNames)

#读取评分数据表,并指定列名
rNames = ['user_id','movie_id','rating','timestamp']
ratings = pd.read_table(r'E:\python\pythonDataAnalysis\pydata-book-master\ch02\movielens\ratings.dat',sep='::',header=None,names=rNames)

#读取评分数据表,并指定列名
moviesNames = ['movie_id','title','genres']
movies = pd.read_table('E:\python\pythonDataAnalysis\pydata-book-master\ch02\movielens\movies.dat',sep='::',header=None,names=moviesNames)


C:\Program Files\anaconda\lib\site-packages\ipykernel\__main__.py:5: ParserWarning: Falling back to the 'python' engine because the 'c' engine does not support regex separators (separators > 1 char and different from '\s+' are interpreted as regex); you can avoid this warning by specifying engine='python'.
C:\Program Files\anaconda\lib\site-packages\ipykernel\__main__.py:9: ParserWarning: Falling back to the 'python' engine because the 'c' engine does not support regex separators (separators > 1 char and different from '\s+' are interpreted as regex); you can avoid this warning by specifying engine='python'.
C:\Program Files\anaconda\lib\site-packages\ipykernel\__main__.py:13: ParserWarning: Falling back to the 'python' engine because the 'c' engine does not support regex separators (separators > 1 char and different from '\s+' are interpreted as regex); you can avoid this warning by specifying engine='python'.


user[:5]


user_idgenderageoccupationzip
01F11048067
12M561670072
23M251555117
34M45702460
45M252055455
ratings[:5]


user_idmovie_idratingtimestamp
0111935978300760
116613978302109
219143978301968
3134084978300275
4123555978824291
movies[:5]


movie_idtitlegenres
01Toy Story (1995)Animation|Children’s|Comedy
12Jumanji (1995)Adventure|Children’s|Fantasy
23Grumpier Old Men (1995)Comedy|Romance
34Waiting to Exhale (1995)Comedy|Drama
45Father of the Bride Part II (1995)Comedy

1.2 根据性别计算某部电影的平均得分

user表中有性别和年龄,movies表中有电影标题,ratings表中有得分,因此达到题目要求需将三个表融合在一起

data = pd.merge(pd.merge(ratings,user),movies)
data.head(5)


user_idmovie_idratingtimestampgenderageoccupationziptitlegenres
0111935978300760F11048067One Flew Over the Cuckoo’s Nest (1975)Drama
1211935978298413M561670072One Flew Over the Cuckoo’s Nest (1975)Drama
21211934978220179M251232793One Flew Over the Cuckoo’s Nest (1975)Drama
31511934978199279M25722903One Flew Over the Cuckoo’s Nest (1975)Drama
41711935978158471M50195350One Flew Over the Cuckoo’s Nest (1975)Drama
#计算不同性别对每部电影的平均得分
mean_ratings = pd.pivot_table(data,values = 'rating',index = ['title'],columns = ['gender'],aggfunc = 'mean')
mean_ratings[:5]


genderFM
title
$1,000,000 Duck (1971)3.3750002.761905
‘Night Mother (1986)3.3888893.352941
‘Til There Was You (1997)2.6756762.733333
‘burbs, The (1989)2.7934782.962085
…And Justice for All (1979)3.8285713.689024
#查看每部电影在不同性别下的评分条数
data.groupby(['title','gender']).size().unstack()[:10]


genderFM
title
$1,000,000 Duck (1971)16.021.0
‘Night Mother (1986)36.034.0
‘Til There Was You (1997)37.015.0
‘burbs, The (1989)92.0211.0
…And Justice for All (1979)35.0164.0
1-900 (1994)1.01.0
10 Things I Hate About You (1999)232.0468.0
101 Dalmatians (1961)187.0378.0
101 Dalmatians (1996)150.0214.0
12 Angry Men (1957)141.0475.0
#选择评论条数大于250 的电影
numComm_by_title = data.groupby(['title']).size()
numComm_by_title[:5]#title为index列


title
$1,000,000 Duck (1971)            37
'Night Mother (1986)              70
'Til There Was You (1997)         52
'burbs, The (1989)               303
...And Justice for All (1979)    199
dtype: int64


active_titles = numComm_by_title.index[numComm_by_title >= 250]
print active_titles.dtype
active_titles.size


object
1216


active_titles[:5]


Index([u''burbs, The (1989)', u'10 Things I Hate About You (1999)',
u'101 Dalmatians (1961)', u'101 Dalmatians (1996)',
u'12 Angry Men (1957)'],
dtype='object', name=u'title')


#ix既可以对行索引,也可以对列索引,可以使用数字序号,还可以使用index关键字
mean_ratings = mean_ratings.ix[active_titles]
#查看mean_ratings的详细信息可以通过mean_ratings?还可以通过help(mean_ratings)
mean_ratings[:5]


genderFM
title
‘burbs, The (1989)2.7934782.962085
10 Things I Hate About You (1999)3.6465523.311966
101 Dalmatians (1961)3.7914443.500000
101 Dalmatians (1996)3.2400002.911215
12 Angry Men (1957)4.1843974.328421

1.3 查看女性最喜欢那部电影?

top_female_ratings = mean_ratings.sort_index(by='F',ascending = False)
top_female_ratings[:10]


C:\Program Files\anaconda\lib\site-packages\ipykernel\__main__.py:1: FutureWarning: by argument to sort_index is deprecated, pls use .sort_values(by=...)
if __name__ == '__main__':


genderFM
title
Close Shave, A (1995)4.6444444.473795
Wrong Trousers, The (1993)4.5882354.478261
Sunset Blvd. (a.k.a. Sunset Boulevard) (1950)4.5726504.464589
Wallace & Gromit: The Best of Aardman Animation (1996)4.5631074.385075
Schindler’s List (1993)4.5626024.491415
Shawshank Redemption, The (1994)4.5390754.560625
Grand Day Out, A (1992)4.5378794.293255
To Kill a Mockingbird (1962)4.5366674.372611
Creature Comforts (1990)4.5138894.272277
Usual Suspects, The (1995)4.5133174.518248

1.3 计算男女之间同一个电影评分差距最大的电影

那些电影最能反映男女之间差别,不是评分最高的,也不是最低的,而是评分差距最大的,如何找出?请看下边代码~

#加上一列存放男女之间评分差的列在透视表中
mean_ratings['diff'] = mean_ratings['M']-mean_ratings['F']
mean_ratings['diff'][:10]


title
'burbs, The (1989)                     0.168607
10 Things I Hate About You (1999)     -0.334586
101 Dalmatians (1961)                 -0.291444
101 Dalmatians (1996)                 -0.328785
12 Angry Men (1957)                    0.144024
13th Warrior, The (1999)               0.056000
2 Days in the Valley (1996)           -0.244076
20,000 Leagues Under the Sea (1954)    0.039102
2001: A Space Odyssey (1968)           0.304156
2010 (1984)                           -0.033097
Name: diff, dtype: float64


mean_ratings_M =mean_ratings.sort_index(by='diff',ascending = False)


   C:\Program Files\anaconda\lib\site-packages\ipykernel\__main__.py:1: FutureWarning: by argument to sort_index is deprecated, pls use .sort_values(by=...)
if __name__ == '__main__':


mean_ratings_M[:5]#男性更喜欢的电影


genderFMdiff
title
Good, The Bad and The Ugly, The (1966)3.4949494.2213000.726351
Kentucky Fried Movie, The (1977)2.8787883.5551470.676359
Dumb & Dumber (1994)2.6979873.3365950.638608
Longest Day, The (1962)3.4117654.0314470.619682
Cable Guy, The (1996)2.2500002.8637870.613787
#女性更喜欢的电影
mean_ratings_F = mean_ratings_M[::-1]
mean_ratings_F[:10]


genderFMdiff
title
Dirty Dancing (1987)3.7903782.959596-0.830782
Jumpin’ Jack Flash (1986)3.2547172.578358-0.676359
Grease (1978)3.9752653.367041-0.608224
Little Women (1994)3.8705883.321739-0.548849
Steel Magnolias (1989)3.9017343.365957-0.535777
Anastasia (1997)3.8000003.281609-0.518391
Rocky Horror Picture Show, The (1975)3.6730163.160131-0.512885
Color Purple, The (1985)4.1581923.659341-0.498851
Age of Innocence, The (1993)3.8270683.339506-0.487561
Free Willy (1993)2.9213482.438776-0.482573

1.4 计算分歧最大的电影

仅从电影评分本身找出分歧最大的电影可以计算每部电影的评分方差或者标准差

#求每部电影的评分标准差
ratings_title_std = data.groupby(['title'])['rating'].std()
print type(ratings_title_std)
ratings_title_std[:5]


<class 'pandas.core.series.Series'>

title
$1,000,000 Duck (1971)           1.092563
'Night Mother (1986)             1.118636
'Til There Was You (1997)        1.020159
'burbs, The (1989)               1.107760
...And Justice for All (1979)    0.878110
Name: rating, dtype: float64


#对Series用order对值排序,还可以用sort_index对列排序
ratings_title_std_sort  = ratings_title_std.order(ascending = False)
ratings_title_std_sort[:5]


C:\Program Files\anaconda\lib\site-packages\ipykernel\__main__.py:1: FutureWarning: order is deprecated, use sort_values(...)
if __name__ == '__main__':

title
Foreign Student (1994)                                             2.828427
Criminal Lovers (Les Amants Criminels) (1999)                      2.309401
Identification of a Woman (Identificazione di una donna) (1982)    2.121320
Sunset Park (1996)                                                 2.121320
Eaten Alive (1976)                                                 2.121320
Name: rating, dtype: float64


2 总结

本篇博客重点介绍了pandas部分功能,包括分组.groupby()、.pivot_table()、.sort_index()等方法的运用,能够快速的对数据进行统计和展示,相似的可视化工具包括excel、tableau等。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  python 数据分析