Python数据科学包(三)----- Pandas后续

本文详细介绍了使用Pandas进行电影数据分析的过程,包括数据读取、合并、按性别查看电影平均评分、发现男女意见差距最大的电影、活跃电影排行榜、高分电影筛选,以及核心数据结构如Series、DataFrame和Panel的创建、选择和运算。通过实例展示了Pandas在数据处理和分析中的强大功能。
摘要由CSDN通过智能技术生成

一. 电影数据分析

1. 数据读取

import pandas as pd
unames = ('user_id','gender','age','occupation','zip')
users = pd.read_table('/Users/yizhou/Desktop/ml-1m/users.dat',sep='::',header=None,names=unames)
print(len(users))
6040
users.head(5)
Out[7]: 
   user_id gender  age  occupation    zip
0        1      F    1          10  48067
1        2      M   56          16  70072
2        3      M   25          15  55117
3        4      M   45           7  02460
4        5      M   25          20  55455
rating_names = ['user_id', 'movie_id', 'rating', 'timestamp']
  ...: ratings = pd.read_table('/Users/yizhou/Desktop/ml-1m/ratings.dat', sep='::', header=None, names=rating_names, engine='python')
  ...: 
  ...: movie_names = ['movie_id', 'title', 'genres']
  ...: movies = pd.read_table('/Users/yizhou/Desktop/ml-1m/movies.dat', sep='::', header=None, names=movie_names, engine='python')
print(len(ratings))
1000209
ratings.head(5)
Out[10]: 
   user_id  movie_id  rating  timestamp
0        1      1193       5  978300760
1        1       661       3  978302109
2        1       914       3  978301968
3        1      3408       4  978300275
4        1      2355       5  978824291
print(len(movies))
3883
movies.head(5)
Out[12]: 
   movie_id                               title                        genres
0         1                    Toy Story (1995)   Animation|Children's|Comedy
1         2                      Jumanji (1995)  Adventure|Children's|Fantasy
2         3             Grumpier Old Men (1995)                Comedy|Romance
3         4            Waiting to Exhale (1995)                  Comedy|Drama
4         5  Father of the Bride Part II (1995)                        Comedy

2. 数据合并

data = pd.merge(pd.merge(users,ratings),movies)
print(len(data))
1000209
data.head(10)
Out[15]: 
   user_id gender  ...                                   title  genres
0        1      F  ...  One Flew Over the Cuckoo's Nest (1975)   Drama
1        2      M  ...  One Flew Over the Cuckoo's Nest (1975)   Drama
2       12      M  ...  One Flew Over the Cuckoo's Nest (1975)   Drama
3       15      M  ...  One Flew Over the Cuckoo's Nest (1975)   Drama
4       17      M  ...  One Flew Over the Cuckoo's Nest (1975)   Drama
5       18      F  ...  One Flew Over the Cuckoo's Nest (1975)   Drama
6       19      M  ...  One Flew Over the Cuckoo's Nest (1975)   Drama
7       24      F  ...  One Flew Over the Cuckoo's Nest (1975)   Drama
8       28      F  ...  One Flew Over the Cuckoo's Nest (1975)   Drama
9       33      M  ...  One Flew Over the Cuckoo's Nest (1975)   Drama
[10 rows x 10 columns]
data[data.user_id == 1]
Out[16]: 
       user_id  ...                                         genres
0            1  ...                                          Drama
1725         1  ...                   Animation|Children's|Musical
2250         1  ...                                Musical|Romance
2886         1  ...                                          Drama
4201         1  ...                    Animation|Children's|Comedy
5904         1  ...                Action|Adventure|Comedy|Romance
8222         1  ...                         Action|Adventure|Drama
8926         1  ...                                   Comedy|Drama
10278        1  ...                   Animation|Children's|Musical
11041        1  ...             Adventure|Children's|Drama|Musical
12759        1  ...                   Animation|Children's|Musical
13819        1  ...                                        Musical
14006        1  ...                                          Drama
14386        1  ...                                         Comedy
15859        1  ...                                        Musical
16741        1  ...                                         Comedy
18472        1  ...                           Animation|Children's
18914        1  ...                           Animation|Children's
19503        1  ...                                          Drama
20183        1  ...                                 Comedy|Fantasy
21674        1  ...                                         Comedy
22832        1  ...                                      Animation
23270        1  ...                                  Comedy|Sci-Fi
25853        1  ...                                      Drama|War
28157        1  ...                                        Romance
28501        1  ...           Animation|Children's|Musical|Romance
28883        1  ...                Children's|Drama|Fantasy|Sci-Fi
31152        1  ...                                  Drama|Romance
32698        1  ...                                          Drama
32771        1  ...                      Animation|Comedy|Thriller
33428        1  ...                           Animation|Children's
34073        1  ...                                       
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值