為電影指派整數 id
現在也用同樣的方法處理電影。接著把新的使用者 ID 與電影 ID 合併成一個 dataframe。
本練習屬於課程
使用 PySpark 打造推薦引擎
練習說明
- 使用
.select()與.distinct()方法,從ratingsdataframe 萃取所有不重複的Movie。 - 使用
coalesce()將moviesdataframe 重新分割為 1 個分割。 - 完成提供的部分程式碼,為每部電影指派唯一的整數 ID。將新欄位命名為
movieId,並對產生的 dataframe 呼叫.persist()方法。 - 先將
ratingsdataframe 與usersdataframe 連接,再與moviesdataframe 連接。把結果命名為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()