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 zip01 F 1104806712 M 56167007223 M 25155511734 M 4570246045 M 252055455
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
011193597830076011661397830210921914397830196831340849783002754123555978824291print(len(movies))3883
movies.head(5)
Out[12]:
movie_id title genres
01 Toy Story (1995) Animation|Children's|Comedy
12 Jumanji (1995) Adventure|Children's|Fantasy
23 Grumpier Old Men (1995) Comedy|Romance
34 Waiting to Exhale (1995) Comedy|Drama
45 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
01 F ... One Flew Over the Cuckoo's Nest (1975) Drama
12 M ... One Flew Over the Cuckoo's Nest (1975) Drama
212 M ... One Flew Over the Cuckoo's Nest (1975) Drama
315 M ... One Flew Over the Cuckoo's Nest (1975) Drama
417 M ... One Flew Over the Cuckoo's Nest (1975) Drama
518 F ... One Flew Over the Cuckoo's Nest (1975) Drama
619 M ... One Flew Over the Cuckoo's Nest (1975) Drama
724 F ... One Flew Over the Cuckoo's Nest (1975) Drama
828 F ... One Flew Over the Cuckoo's Nest (1975) Drama
933 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
01... Drama
17251... Animation|Children's|Musical
22501... Musical|Romance
28861... Drama
42011... Animation|Children's|Comedy
59041... Action|Adventure|Comedy|Romance
82221... Action|Adventure|Drama
89261... Comedy|Drama
102781... Animation|Children's|Musical
110411... Adventure|Children's|Drama|Musical
127591... Animation|Children's|Musical
138191... Musical
140061... Drama
143861... Comedy
158591... Musical
167411... Comedy
184721... Animation|Children's
189141... Animation|Children's
195031... Drama
201831... Comedy|Fantasy
216741... Comedy
228321... Animation
232701... Comedy|Sci-Fi
258531... Drama|War
281571... Romance
285011... Animation|Children's|Musical|Romance
288831... Children's|Drama|Fantasy|Sci-Fi
311521... Drama|Romance
326981... Drama
327711... Animation|Comedy|Thriller
334281... Animation|Children's
340731...