Polars – filling nulls in a column based on values from another

Question:

I’ve been trying to learn Polars and one thing I do with Pandas I still couldn’t find out how to do is to fill nulls from a column using a dictionary to compare the values in another column. For example:

import pandas as pd
import numpy as np

df = pd.DataFrame({"a":[np.nan,2,3,np.nan,5],
                  "b":[1,2,3,4,5],
                  })

dictionary={1:1,
           2:4,
           3:6,
           4:8,
           5:10}

df["a"]=df["a"].fillna(df["b"].map(dictionary))

This would result the following DataFrame:

a b
1.0 1
2.0 2
3.0 3
8.0 4
5.0 5

So my question is: How can I do something similar using Polars? Thanks in advance!

Asked By: Gabriel Zinato

||

Answers:

Here’s one way, using when and map_dict:

df = pl.DataFrame(
    {
        "a": [None, 2, 3, None, 5],
        "b": [1, 2, 3, 4, 5],
    }
)

dictionary = {1: 1, 2: 4, 3: 6, 4: 8, 5: 10}

df.with_columns(
    a=pl.when(pl.col("a").is_null())
    .then(pl.col("b").map_dict(dictionary))
    .otherwise(pl.col("a"))
)
Answered By: Wayoshi
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.