polars.Expr.str.replace_many#
- Expr.str.replace_many(patterns: IntoExpr | Mapping[str, str], replace_with: IntoExpr | NoDefault = <no_default>, *, ascii_case_insensitive: bool = False) Expr[source]#
使用Aho-Corasick算法替换多个匹配项。
- Parameters:
- patterns
用于搜索和替换的字符串模式。 接受表达式输入。字符串被解析为列名,其他非表达式输入被解析为字面量。还接受模式到其替换的映射,作为
replace_many(pl.Series(mapping.keys()), pl.Series(mapping.values()))的语法糖。- replace_with
替换模式匹配的字符串。 接受表达式输入。非表达式输入被解析为字面量。 长度必须与
patterns的长度匹配或长度为1。这可以 广播,因此支持多对一和多对多。- ascii_case_insensitive
启用ASCII感知的大小写不敏感匹配。 当此选项启用时,搜索将仅对ASCII字母(a-z和A-Z)不区分大小写。
注释
此方法仅支持字符串字面量的匹配,不支持正则表达式匹配。
示例
通过向
patterns和replace_with参数传递等长的序列来替换多个模式。>>> _ = pl.Config.set_fmt_str_lengths(100) >>> _ = pl.Config.set_tbl_width_chars(110) >>> df = pl.DataFrame( ... { ... "lyrics": [ ... "Everybody wants to rule the world", ... "Tell me what you want, what you really really want", ... "Can you feel the love tonight", ... ] ... } ... ) >>> df.with_columns( ... pl.col("lyrics") ... .str.replace_many( ... ["me", "you"], ... ["you", "me"], ... ) ... .alias("confusing") ... ) shape: (3, 2) ┌────────────────────────────────────────────────────┬───────────────────────────────────────────────────┐ │ lyrics ┆ confusing │ │ --- ┆ --- │ │ str ┆ str │ ╞════════════════════════════════════════════════════╪═══════════════════════════════════════════════════╡ │ Everybody wants to rule the world ┆ Everybody wants to rule the world │ │ Tell me what you want, what you really really want ┆ Tell you what me want, what me really really want │ │ Can you feel the love tonight ┆ Can me feel the love tonight │ └────────────────────────────────────────────────────┴───────────────────────────────────────────────────┘
通过向
replace_with参数传递一个字符串或长度为1的序列,广播替换多个模式。>>> _ = pl.Config.set_fmt_str_lengths(100) >>> df = pl.DataFrame( ... { ... "lyrics": [ ... "Everybody wants to rule the world", ... "Tell me what you want, what you really really want", ... "Can you feel the love tonight", ... ] ... } ... ) >>> df.with_columns( ... pl.col("lyrics") ... .str.replace_many( ... ["me", "you", "they"], ... "", ... ) ... .alias("removes_pronouns") ... ) shape: (3, 2) ┌────────────────────────────────────────────────────┬────────────────────────────────────────────┐ │ lyrics ┆ removes_pronouns │ │ --- ┆ --- │ │ str ┆ str │ ╞════════════════════════════════════════════════════╪════════════════════════════════════════════╡ │ Everybody wants to rule the world ┆ Everybody wants to rule the world │ │ Tell me what you want, what you really really want ┆ Tell what want, what really really want │ │ Can you feel the love tonight ┆ Can feel the love tonight │ └────────────────────────────────────────────────────┴────────────────────────────────────────────┘
传递带有模式和替换的映射也作为语法糖支持。
>>> _ = pl.Config.set_fmt_str_lengths(100) >>> _ = pl.Config.set_tbl_width_chars(110) >>> df = pl.DataFrame( ... { ... "lyrics": [ ... "Everybody wants to rule the world", ... "Tell me what you want, what you really really want", ... "Can you feel the love tonight", ... ] ... } ... ) >>> mapping = {"me": "you", "you": "me", "want": "need"} >>> df.with_columns( ... pl.col("lyrics").str.replace_many(mapping).alias("confusing") ... ) shape: (3, 2) ┌────────────────────────────────────────────────────┬───────────────────────────────────────────────────┐ │ lyrics ┆ confusing │ │ --- ┆ --- │ │ str ┆ str │ ╞════════════════════════════════════════════════════╪═══════════════════════════════════════════════════╡ │ Everybody wants to rule the world ┆ Everybody needs to rule the world │ │ Tell me what you want, what you really really want ┆ Tell you what me need, what me really really need │ │ Can you feel the love tonight ┆ Can me feel the love tonight │ └────────────────────────────────────────────────────┴───────────────────────────────────────────────────┘