2

这个截图是合并的movielens数据集的样本,我有两个问题:

  1. 如果我只想提取用户 191 的电影 ID、标题、流派和收视率,我该怎么做?
  2. 如何列出每部电影标题末尾的年份?

任何指南将不胜感激。

Movielens 数据集的屏幕截图

4

3 回答 3

2

第一个问题;使用布尔选择

df[df['userid']=='191']

第二个问题#使用正则表达式提取括号之间的短语

df['Year']=df.title.str.extract('\((.*?)\)')
于 2020-06-29T21:13:52.407 回答
1

为每个唯一用户创建一个数据框字典

  • 使用正则表达式从括号之间'\((\d+)\)'提取数字 ,\d并将值分配给movies['Year']
  • 这不是为不同用户重复过滤数据框,而是将每个用户作为键添加到字典中,值将是为该用户过滤的数据框。
    • 有 162541 个唯一userId值,因此不要使用,而是使用您感兴趣df.userId.unique()的特定值的列表。userId
  • 另请参阅与此 MovieLens 数据集相关的How to rotate seaborn barplot x-axis tick labels的答案。
# question 1: create a column for the year extracted from the title
# extracts the digits between parenthesis
# does not change the title column
df['Year'] = df.title.str.extract('\((\d+)\)')

# create dict of dataframes for each user
userid_movies = dict()
for user in [10, 15, 191]:  # df.userId.unique() = 162541 unique users
    data = df[df.userId == user]
    userid_movies[user] = data

# get data for user 191; assumes ids are int. if not, use '191'
userid_movies[191]  # if you're using jupyter, don't use print

例子

import pandas as pd

# load movies
movies = pd.read_csv('data/ml-25m/movies.csv')

# extract year
movies['Year'] = movies.title.str.extract('\((\d+)\)')

# display head
   movieId                               title                                       genres  Year
0        1                    Toy Story (1995)  Adventure|Animation|Children|Comedy|Fantasy  1995
1        2                      Jumanji (1995)                   Adventure|Children|Fantasy  1995
2        3             Grumpier Old Men (1995)                               Comedy|Romance  1995
3        4            Waiting to Exhale (1995)                         Comedy|Drama|Romance  1995
4        5  Father of the Bride Part II (1995)                                       Comedy  1995

# load ratings
ratings = pd.read_csv('data/ml-25m/ratings.csv')

# merge on movieId
df = pd.merge(movies, ratings, on='movieId').reset_index(drop=True)

# display df
   movieId             title                                       genres  Year  userId  rating   timestamp
0        1  Toy Story (1995)  Adventure|Animation|Children|Comedy|Fantasy  1995       2     3.5  1141415820
1        1  Toy Story (1995)  Adventure|Animation|Children|Comedy|Fantasy  1995       3     4.0  1439472215
2        1  Toy Story (1995)  Adventure|Animation|Children|Comedy|Fantasy  1995       4     3.0  1573944252
3        1  Toy Story (1995)  Adventure|Animation|Children|Comedy|Fantasy  1995       5     4.0   858625949
4        1  Toy Story (1995)  Adventure|Animation|Children|Comedy|Fantasy  1995       8     4.0   890492517

# dict of dataframes
# there are 162541 unique userId values, so instead of using df.userId.unique()
# use a list of the specific Id values you're interested in
userid_movies = dict()
for user in [10, 15, 191]:
    data = df[df.userId == user].reset_index(drop=True)
    userid_movies[user] = data

# display(userid_movies[191].head())
   movieId                                  title                                              genres  Year  userId  rating   timestamp
0    68135                        17 Again (2009)                                        Comedy|Drama  2009     191     3.0  1473704208
1    68791            Terminator Salvation (2009)                    Action|Adventure|Sci-Fi|Thriller  2009     191     5.0  1473704167
2    68954                              Up (2009)                  Adventure|Animation|Children|Drama  2009     191     4.0  1473703994
3    69406                   Proposal, The (2009)                                      Comedy|Romance  2009     191     4.0  1473704198
4    69644  Ice Age: Dawn of the Dinosaurs (2009)  Action|Adventure|Animation|Children|Comedy|Romance  2009     191     1.5  1473704242

于 2020-06-29T21:31:54.493 回答
1

对于问题的第一部分,您可以过滤数据框。

user191 = df.loc[df['userId']==191]

对于您问题的第二部分,年份似乎总是在最后,因此您可以取出字符串的最后一部分并删除括号。

df['Year'] = df['title'].str[-5:].str.replace(')','')
于 2020-06-29T21:16:22.133 回答