Skip to content

Take two sorted DataFrames and merge them by the sorted key

Description

The output of this operation will also be sorted. It is the callers responsibility that the frames are sorted by that key, otherwise the output will not make sense. The schemas of both DataFrames must be equal.

Usage

<DataFrame>$merge_sorted(other, key)

Arguments

other Other DataFrame that must be merged.
key Key that is sorted.

Value

A polars DataFrame

Examples

library("polars")

df1 <- pl$DataFrame(
  name = c("steve", "elise", "bob"),
  age = c(42, 44, 18)
)$sort("age")

df2 <- pl$DataFrame(
  name = c("anna", "megan", "steve", "thomas"),
  age = c(21, 33, 42, 20)
)$sort("age")

df1$merge_sorted(df2, key = "age")
#> shape: (7, 2)
#> ┌────────┬──────┐
#> │ name   ┆ age  │
#> │ ---    ┆ ---  │
#> │ str    ┆ f64  │
#> ╞════════╪══════╡
#> │ bob    ┆ 18.0 │
#> │ thomas ┆ 20.0 │
#> │ anna   ┆ 21.0 │
#> │ megan  ┆ 33.0 │
#> │ steve  ┆ 42.0 │
#> │ steve  ┆ 42.0 │
#> │ elise  ┆ 44.0 │
#> └────────┴──────┘