You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何提取并合并Polars中可能不存在的深度嵌套值?

解决Polars中嵌套Struct字段缺失报错的问题

要替代str.json_path_match的写法,同时避免字段不存在时抛出StructFieldNotFoundError,可以用以下两种方案:

方案1:使用struct.try_field()(推荐,Polars 0.19.0+)

try_field()是Polars专门为容错场景设计的方法,当指定字段不存在时会返回null而非报错,完美匹配你原来json_path_match的逻辑。

修改后的完整代码如下:

lf.with_columns(
    [
        pl.coalesce(
            [
                pl.col("a"),
                pl.col("some_struct").struct.try_field("foo"),
                pl.col("some_struct").struct.try_field("bar").struct.try_field("baz"),
            ]
        )
        .str.split("(")
        .list.first()
        .str.strip_chars()
        .alias("computed_col_a"),
        pl.coalesce(
            [
                pl.col("b"),
                pl.col("some_struct").struct.try_field("blah1"),
                pl.col("some_struct").struct.try_field("blah2"),
                pl.col("some_struct").struct.try_field("blah3"),
            ]
        ).alias("computed_col_b"),
    ]
)

方案2:使用field_exists()做条件判断(兼容旧版本Polars)

如果你的Polars版本低于0.19.0,没有try_field方法,可以通过struct.field_exists()先判断字段是否存在,再用when/then/otherwise返回对应值或null:

以提取bar.baz为例,写法如下:

pl.when(pl.col("some_struct").struct.field_exists("bar"))
  .then(
      pl.when(pl.col("some_struct").struct.field("bar").struct.field_exists("baz"))
      .then(pl.col("some_struct").struct.field("bar").struct.field("baz"))
      .otherwise(None)
  )
  .otherwise(None)

替换到完整代码中:

lf.with_columns(
    [
        pl.coalesce(
            [
                pl.col("a"),
                pl.when(pl.col("some_struct").struct.field_exists("foo"))
                  .then(pl.col("some_struct").struct.field("foo"))
                  .otherwise(None),
                pl.when(pl.col("some_struct").struct.field_exists("bar"))
                  .then(
                      pl.when(pl.col("some_struct").struct.field("bar").struct.field_exists("baz"))
                      .then(pl.col("some_struct").struct.field("bar").struct.field("baz"))
                      .otherwise(None)
                  )
                  .otherwise(None),
            ]
        )
        .str.split("(")
        .list.first()
        .str.strip_chars()
        .alias("computed_col_a"),
        pl.coalesce(
            [
                pl.col("b"),
                pl.when(pl.col("some_struct").struct.field_exists("blah1"))
                  .then(pl.col("some_struct").struct.field("blah1"))
                  .otherwise(None),
                pl.when(pl.col("some_struct").struct.field_exists("blah2"))
                  .then(pl.col("some_struct").struct.field("blah2"))
                  .otherwise(None),
                pl.when(pl.col("some_struct").struct.field_exists("blah3"))
                  .then(pl.col("some_struct").struct.field("blah3"))
                  .otherwise(None),
            ]
        ).alias("computed_col_b"),
    ]
)

两种方案都能保证在字段缺失时返回null,让coalesce可以正常遍历备选值,同时用Struct原生方法替代字符串解析,性能比json_path_match更优。

内容的提问来源于stack exchange,提问作者dsully

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.12 07:53:18