開始使用免費開始

為電影指派整數 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()
編輯並執行程式碼