实现这一点的极坐标表达方式是什么,
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
有一个功能请求允许这样做,但尚未实现:
可以使用窗口函数进行模拟,例如
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 │
└───────────────────────────────┴───────────────────────────┴──────────────┘