Update 2024-07-09
Polars has dedicated replace and replace_strict expressions.
Old answer
Mapping a python dictionary over a polars Series should always be considered an anti-pattern. This will be terribly slow and what you want is semantically equal to a join.
Use joins. They are heavily optimized, multithreaded and don't use python.
Example
import polars as pl
dic = { 1: 'a', 2: 'b', 3: 'c' }
mapper = pl.DataFrame({
"keys": list(dic.keys()),
"values": list(dic.values())
})
pl.Series([1, 2, 3, 4]).to_frame("keys").join(mapper, on="keys", how="left").to_series(1)
Series: 'values' [str]
[
"a"
"b"
"c"
null
]
Answer from ritchie46 on Stack OverflowUpdate 2024-07-09
Polars has dedicated replace and replace_strict expressions.
Old answer
Mapping a python dictionary over a polars Series should always be considered an anti-pattern. This will be terribly slow and what you want is semantically equal to a join.
Use joins. They are heavily optimized, multithreaded and don't use python.
Example
import polars as pl
dic = { 1: 'a', 2: 'b', 3: 'c' }
mapper = pl.DataFrame({
"keys": list(dic.keys()),
"values": list(dic.values())
})
pl.Series([1, 2, 3, 4]).to_frame("keys").join(mapper, on="keys", how="left").to_series(1)
Series: 'values' [str]
[
"a"
"b"
"c"
null
]
Polars has the Expr.replace_strict method and the Series.replace_strict method which can be used as follows:
mapping_dict = {1: "a", 2: "b", 3: "c"}
# pl.Series.replace_strict
pl.Series([1, 2, 3, 4]).replace_strict(mapping_dict, default=None)
# pl.Expr.replace_strict
pl_df = pl.Series(name="to_map_col", values=[1, 2, 3, 4]).to_frame()
pl_df.with_columns(pl.col("to_map_col").replace_strict(mapping_dict, default=None))
Polars mapping
Map DataFrame Column using Python Dicts
Change default behavior `map_dict`
Dict/Hashmap lookup expression
I am relatively new to python. I’m trying to map a column of integers to string values defined in a dictionary.
I’m using polars and this is seemingly more difficult that I first anticipated. can anyone give advice on how to do this?