開始使用免費開始

重新命名國家欄位

在報表中,struct 的欄位 codename 太過籠統。請先把它們改名為 country_codecountry_name,然後再展開。

你仍在使用相同的 movies DataFrame。

本練習屬於課程

使用 Polars 擴充與最佳化資料管線

檢視課程

練習說明

  • 在 struct 上使用正確的方法來重新命名其欄位。
  • 依序命名為 country_codecountry_name

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

result = (
    movies
    .with_columns(
        # Rename the two struct fields
        pl.col("production_country")
        .struct.____(["____", "____"])
        .alias("production_country")
    )
    .unnest("production_country")
    .select("movie_title", "country_code", "country_name")
    .head(8)
)
print(result)
編輯並執行程式碼