-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow dictionary in .str.replace() and .str.replace_all() #11418
Comments
hmm interesting idea but I can also see how this could be confusing.
If you try to replace whole values (in your example |
As @JulianCologne points out, they are slightly different operations. Passing multiple patterns/replacements usually implies the replacements happen in isolation / independently of each other. Whereas with chaining, each call is processing the output from the previous call. As for chaining, I was trying to figure out if it was possible to build a helper function. i.e. turn replacements = {'one': 'two', 'three': 'four'}
expr = pl.col('field1', 'field2').str.replace_all into:
But I'm not sure if that is possible? With expr and string, we could split on expr, function = pl.col('field1', 'field2'), 'str.replace_all' The actual expr, function = pl.col('field1', 'field2'), pl.expr.string.ExprStringNameSpace.replace_all But that's somewhat awkward. |
It seems that standard replace allows dicts: import polars as pl
dff = pl.DataFrame({'a':['a','b','c']})
mapping = {'a':'apple', 'b':'banana'}
dff.with_columns(pl.col('a').replace(mapping, default=pl.lit('***')).alias('aa')) |
Description
It would be nice to have instead of chained replace_all's:
using a dict, something like:
The text was updated successfully, but these errors were encountered: