ComputeError:尚不支持“str.replace”表达式中的动态模式长度

问题描述 投票:0回答:1

实现这一点的极坐标表达方式是什么,

df = pl.from_repr("""
┌───────────────────────────────┬───────────────────────────┐
│ document_url                  ┆ matching_seed_url         │
│ ---                           ┆ ---                       │
│ str                           ┆ str                       │
╞═══════════════════════════════╪═══════════════════════════╡
│ https://document_url.com/1234 ┆ https://document_url.com/ │
│ https://document_url.com/5678 ┆ https://document_url.com/ │
└───────────────────────────────┴───────────────────────────┘""")
df = df.with_columns(
    pl.when(pl.col("matching_seed_url").is_not_null())
    .then(pl.col("document_url").str.replace(pl.col("matching_seed_url"), ""))
    .otherwise(pl.lit(""))
    .alias("extracted_id"))

我明白了,

ComputeError: dynamic pattern length in 'str.replace' expressions is not supported yet

如何在这里提取1234、5678

python-polars
1个回答
0
投票

有一个功能请求允许这样做,但尚未实现:

可以使用窗口函数进行模拟,例如

df.with_columns(
    pl.col.document_url.str.replace(pl.col.matching_seed_url.first(), "")
      .over(pl.col.matching_seed_url)
      .fill_null("")
      .alias("extracted_id")
)

如果不需要正则表达式,也可以使用

.str.replace_many()

df.with_columns(
    pl.col.document_url.str.replace_many(pl.col.matching_seed_url, "")
      .fill_null("")
      .alias("extracted_id")
)
shape: (2, 3)
┌───────────────────────────────┬───────────────────────────┬──────────────┐
│ document_url                  ┆ matching_seed_url         ┆ extracted_id │
│ ---                           ┆ ---                       ┆ ---          │
│ str                           ┆ str                       ┆ str          │
╞═══════════════════════════════╪═══════════════════════════╪══════════════╡
│ https://document_url.com/1234 ┆ https://document_url.com/ ┆ 1234         │
│ https://document_url.com/5678 ┆ https://document_url.com/ ┆ 5678         │
└───────────────────────────────┴───────────────────────────┴──────────────┘
© www.soinside.com 2019 - 2024. All rights reserved.