Search code examples
python-polars

replace part of string with values from dictionary?


Loving the Polars library for its fantastic speed and easy syntax!

Struggling with this question - is there an analogue in Polars for the Pandas code below? Would like to replace strings using a dictionary.

Tried using this expression, but it returns 'TypeError: 'dict' object is not callable'

pl.col("List").str.replace_all(lambda key: key,dict())

Trying to replace the Working Pandas code below with a Polars expression

df = pd.DataFrame({'List':[
    'Systems',
    'Software',
    'Cleared'
    ]})

dic = {
    'Systems':'Sys'
    ,'Software':'Soft'
    ,'Cleared':'Clr'
    }

df["List"] = df["List"].replace(dic, regex=True)

Output:

 List
 0   Sys
 1  Soft
 2   Clr


Solution

  • There is a "stale" feature request for accepting a dictionary:

    One possible workaround is to stack multiple expressions in a loop:

    expr = pl.col("List")
    
    for old, new in dic.items():
        expr = expr.str.replace_all(old, new)
        
    df.with_columns(result = expr)
    
    shape: (3, 2)
    ┌──────────┬────────┐
    │ List     ┆ result │
    │ ---      ┆ ---    │
    │ str      ┆ str    │
    ╞══════════╪════════╡
    │ Systems  ┆ Sys    │
    │ Software ┆ Soft   │
    │ Cleared  ┆ Clr    │
    └──────────┴────────┘
    

    For non-regex cases, there is also .str.replace_many():

    df.with_columns(
       pl.col("List").str.replace_many(
           ["Systems", "Software", "Cleared"],
           ["Sys", "Soft", "Clr"]
       )
       .alias("result")
    )