建立 Object 型別
為了臨時的文字探索,團隊想把每部電影的片名轉成由小寫單字組成的 Python set。由於 set 不是 Polars 的原生型別,請將它存放在 pl.Object 型別的欄位中。
已提供 DataFrame movies,而輔助函式 title_to_word_set(title) 也已定義好,會從片名回傳一個由小寫單字組成的 Python set。
本練習屬於課程
使用 Polars 擴充與最佳化資料管線
練習說明
- 對每個
movie_title執行title_to_word_set,並將回傳的 Python set 存入新欄位。 - 將新欄位命名為
title_word_set。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
result = movies.with_columns(
pl.col("movie_title")
# Convert each title to a set of words
.____(title_to_word_set, return_dtype=pl.____)
.alias("____")
).select("movie_title", "title_word_set").head(8)
print(result)