开始使用免费开始使用

重命名国家字段

在报告中,struct 字段 codename 过于笼统。请先将它们分别重命名为 country_codecountry_name,然后再展开(unnest)。

您仍在使用相同的 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)
编辑并运行代码