Skip to content

Perform a join based on one or multiple (in)equality predicates

Source code

Description

This performs an inner join, so only rows where all predicates are true are included in the result, and a row from either LazyFrame may be included multiple times in the result.

Note that the row order of the input LazyFrames is not preserved.

Usage

<LazyFrame>$join_where(other, ..., suffix = "_right")

Arguments

other LazyFrame to join with.
(In)Equality condition to join the two tables on. When a column name occurs in both tables, the proper suffix must be applied in the predicate. For example, if both tables have a column “x” that you want to use in the conditions, you must refer to the column of the right table as “x\.
suffix Suffix to append to columns with a duplicate name.

Value

A LazyFrame

Examples

library("polars")

east = pl$LazyFrame(
  id = c(100, 101, 102),
  dur = c(120, 140, 160),
  rev = c(12, 14, 16),
  cores = c(2, 8, 4)
)

west = pl$LazyFrame(
  t_id = c(404, 498, 676, 742),
  time = c(90, 130, 150, 170),
  cost = c(9, 13, 15, 16),
  cores = c(4, 2, 1, 4)
)

east$join_where(
  west,
  pl$col("dur") < pl$col("time"),
  pl$col("rev") < pl$col("cost")
)$collect()
#> shape: (5, 8)
#> ┌───────┬───────┬──────┬───────┬───────┬───────┬──────┬─────────────┐
#> │ id    ┆ dur   ┆ rev  ┆ cores ┆ t_id  ┆ time  ┆ cost ┆ cores_right │
#> │ ---   ┆ ---   ┆ ---  ┆ ---   ┆ ---   ┆ ---   ┆ ---  ┆ ---         │
#> │ f64   ┆ f64   ┆ f64  ┆ f64   ┆ f64   ┆ f64   ┆ f64  ┆ f64         │
#> ╞═══════╪═══════╪══════╪═══════╪═══════╪═══════╪══════╪═════════════╡
#> │ 100.0 ┆ 120.0 ┆ 12.0 ┆ 2.0   ┆ 498.0 ┆ 130.0 ┆ 13.0 ┆ 2.0         │
#> │ 100.0 ┆ 120.0 ┆ 12.0 ┆ 2.0   ┆ 676.0 ┆ 150.0 ┆ 15.0 ┆ 1.0         │
#> │ 100.0 ┆ 120.0 ┆ 12.0 ┆ 2.0   ┆ 742.0 ┆ 170.0 ┆ 16.0 ┆ 4.0         │
#> │ 101.0 ┆ 140.0 ┆ 14.0 ┆ 8.0   ┆ 676.0 ┆ 150.0 ┆ 15.0 ┆ 1.0         │
#> │ 101.0 ┆ 140.0 ┆ 14.0 ┆ 8.0   ┆ 742.0 ┆ 170.0 ┆ 16.0 ┆ 4.0         │
#> └───────┴───────┴──────┴───────┴───────┴───────┴──────┴─────────────┘