开始使用免费开始使用

为电影分配整数 ID

现在也对电影做同样的处理。然后将新的用户 ID 和电影 ID 连接成一个 dataframe。

本练习是课程的一部分

使用 PySpark 构建推荐引擎

查看课程

练习说明

  • 使用 .select().distinct() 方法,从 ratings dataframe 中提取所有不重复的 Movie
  • 使用 coalesce()movies dataframe 合并为 1 个分区。
  • 补全给出的部分代码,为每部电影分配唯一的整数 ID。将新列命名为 movieId,并对结果 dataframe 调用 .persist() 方法。
  • 先将 ratings dataframe 与 users dataframe 连接,再与 movies dataframe 连接。将结果命名为 movie_ratings

交互式实操练习

通过完成这段示例代码来试试这个练习。

# Extract the distinct movie id's
movies = ratings.select("____").distinct() 

# Repartition the data to have only one partition.
movies = movies.coalesce(____) 

# Create a new column of movieId integers. 
movies = movies.withColumn("____", monotonically_increasing_id()).____() 

# Join the ratings, users and movies dataframes
movie_ratings = ratings.join(____, "User", "left").join(____, "Movie", "left")
movie_ratings.show()
编辑并运行代码