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!
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"))
)
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!
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"))
)