applypython-polars

Polars apply lambda alternative


I am currently converting my data science project from pandas to polars. I have successfully converted most of it, but I am facing a challenge with a specific part. I am trying to find a polars expression to achieve the following result, which I previously implemented using the 'apply' function in pandas:

data1 = {"a": [1, 2, 3, 4], "b1": [11, 12, 13, 14], "c1" : [31, 32, 33, 34]}
df1_pl = pl.DataFrame(data1)
┌─────┬─────┬─────┐
│ a   ┆ b1  ┆ c1  │
│ --- ┆ --- ┆ --- │
│ i64 ┆ i64 ┆ i64 │
╞═════╪═════╪═════╡
│ 1   ┆ 11  ┆ 31  │
│ 2   ┆ 12  ┆ 32  │
│ 3   ┆ 13  ┆ 33  │
│ 4   ┆ 14  ┆ 34  │
└─────┴─────┴─────┘
weekday = ['Monday', 'Tuesday', 'Wednesday', 'Thursday']

print(df1_pl.with_columns(
weekday=pl.col('a').map_elements(lambda x: weekday[x-1])  
))

Desired Output DataFrame:

┌─────┬─────┬─────┬───────────┐
│ a   ┆ b1  ┆ c1  ┆ weekday   │
│ --- ┆ --- ┆ --- ┆ ---       │
│ i64 ┆ i64 ┆ i64 ┆ str       │
╞═════╪═════╪═════╪═══════════╡
│ 1   ┆ 11  ┆ 31  ┆ Monday    │
│ 2   ┆ 12  ┆ 32  ┆ Tuesday   │
│ 3   ┆ 13  ┆ 33  ┆ Wednesday │
│ 4   ┆ 14  ┆ 34  ┆ Thursday  │
└─────┴─────┴─────┴───────────┘

I am aware that using polars expressions is recommended over using 'map_elements'. Therefore, I am seeking assistance in finding a more efficient polars expression to achieve the same result as shown above. Any guidance on this matter would be highly appreciated. Thank you!


Solution

  • Use replace_strict:

    In [21]: data1 = {"a": [1, 2, 3, 4], "b1": [11, 12, 13, 14], "c1" : [31, 32, 33, 34]}
        ...: df1_pl = pl.DataFrame(data1)
        ...: print(df1_pl)
        ...: weekday = ['Monday', 'Tuesday', 'Wednesday', 'Thursday']
        ...:
        ...: print(df1_pl.with_columns(
        ...: weekday=pl.col('a').replace_strict({idx: val for idx, val in enumerate(weekday, start=1)})
        ...: ))
    shape: (4, 3)
    ┌─────┬─────┬─────┐
    │ a   ┆ b1  ┆ c1  │
    │ --- ┆ --- ┆ --- │
    │ i64 ┆ i64 ┆ i64 │
    ╞═════╪═════╪═════╡
    │ 1   ┆ 11  ┆ 31  │
    │ 2   ┆ 12  ┆ 32  │
    │ 3   ┆ 13  ┆ 33  │
    │ 4   ┆ 14  ┆ 34  │
    └─────┴─────┴─────┘
    shape: (4, 4)
    ┌─────┬─────┬─────┬───────────┐
    │ a   ┆ b1  ┆ c1  ┆ weekday   │
    │ --- ┆ --- ┆ --- ┆ ---       │
    │ i64 ┆ i64 ┆ i64 ┆ str       │
    ╞═════╪═════╪═════╪═══════════╡
    │ 1   ┆ 11  ┆ 31  ┆ Monday    │
    │ 2   ┆ 12  ┆ 32  ┆ Tuesday   │
    │ 3   ┆ 13  ┆ 33  ┆ Wednesday │
    │ 4   ┆ 14  ┆ 34  ┆ Thursday  │
    └─────┴─────┴─────┴───────────┘