polars.LazyFrame.with_context#

LazyFrame.with_context(other: LDF | list[LDF]) LDF[source]#

Add an external context to the computation graph.

This allows expressions to also access columns from DataFrames that are not part of this one.

Parameters:
other

Lazy DataFrame to join with.

Examples

>>> df_a = pl.DataFrame({"a": [1, 2, 3], "b": ["a", "c", None]}).lazy()
>>> df_other = pl.DataFrame({"c": ["foo", "ham"]})
>>> (
...     df_a.with_context(df_other.lazy()).select(
...         [pl.col("b") + pl.col("c").first()]
...     )
... ).collect()
shape: (3, 1)
┌──────┐
│ b    │
│ ---  │
│ str  │
╞══════╡
│ afoo │
├╌╌╌╌╌╌┤
│ cfoo │
├╌╌╌╌╌╌┤
│ null │
└──────┘

Fill nulls with the median from another dataframe >>> train_df = pl.DataFrame( … {“feature_0”: [-1.0, 0, 1], “feature_1”: [-1.0, 0, 1]} … ).lazy() >>> test_df = pl.DataFrame( … {“feature_0”: [-1.0, None, 1], “feature_1”: [-1.0, 0, 1]} … ).lazy() >>> ( … test_df.with_context(train_df.select(pl.all().suffix(“_train”))).select( … pl.col(“feature_0”).fill_null(pl.col(“feature_0_train”).median()) … ) … ).collect() shape: (3, 1) ┌───────────┐ │ feature_0 │ │ — │ │ f64 │ ╞═══════════╡ │ -1.0 │ ├╌╌╌╌╌╌╌╌╌╌╌┤ │ 0.0 │ ├╌╌╌╌╌╌╌╌╌╌╌┤ │ 1.0 │ └───────────┘