시작하기무료로 시작하기

영화에 정수 ID 할당하기

이번에는 영화를 대상으로 같은 작업을 해 보세요. 그런 다음 새로 만든 사용자 ID와 영화 ID를 하나의 데이터프레임으로 조인해 보겠습니다.

이 연습은 강의의 일부입니다

PySpark로 추천 엔진 만들기

강의 보기

연습 안내

  • ratings 데이터프레임에서 .select().distinct() 메서드를 사용해 고유한 Movie만 추출하세요.
  • coalesce()를 사용해 movies 데이터프레임을 하나의 파티션으로 재파티셔닝하세요.
  • 제공된 부분 코드를 완성해 각 영화에 고유한 정수 ID를 할당하세요. 새 열 이름은 movieId로 하고, 결과 데이터프레임에 .persist() 메서드를 호출하세요.
  • ratings 데이터프레임을 users 데이터프레임과 조인한 뒤, 이어서 movies 데이터프레임과 조인하세요. 결과 이름은 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()
코드 편집 및 실행