条件または最も近い値でDataFrameを結合する

Data Transformation with Polars

Liam Brannigan

Data Scientist & Polars Contributor

条件で結合する

ワールドカップ優勝国の単一テーブル

Data Transformation with Polars

条件で結合する

ワールドカップ優勝国のテーブルと、シルバー・ゴールドの最小勝利数のテーブル

Data Transformation with Polars

条件で結合する

ワールドカップ優勝国、レベル、および達成レベル付きの結合テーブル

Data Transformation with Polars

条件で結合する

ワールドカップ優勝国、レベル、達成レベルの結合テーブル。ブラジルの2行が強調表示。

Data Transformation with Polars

嗜好付きのアプリユーザー

restaurants = pl.read_csv("restuarants.csv")
shape: (4, 5)
| business        | location    | review | price | type       |
| ---             | ---         | ---    | ---   | ---        |
| str             | str         | f64    | i64   | str        |
|-----------------|-------------|--------|-------|------------|
| 7burgers        | Wakey Wakey | 4.2    | 15    | restaurant |
| Costa Coffee    | City Point  | 4.5    | 8     | café       |
| Costa Coffee    | Waterloo    | 4.1    | 8     | café       |
| The Queens Head | Denman St.  | 4.7    | 25    | bar        |
Data Transformation with Polars

users テーブル

users = pl.read_csv("app_users.csv")
shape: (3, 3)
| user_name | budget | type       |
| ---       | ---    | ---        |
| str       | i64    | str        |
|-----------|--------|------------|
| Bob       | 12     | café       |
| Alice     | 15     | restaurant |
| Charlie   | 22     | bar        |
Data Transformation with Polars

条件で結合する

restaurants.join_where(


)
Data Transformation with Polars

条件で結合する

restaurants.join_where(
    users,

)
Data Transformation with Polars

条件で結合する

restaurants.join_where(
    users,
    pl.col("type") == pl.col("type_right")
)
Data Transformation with Polars

条件で結合する

restaurants.join_where(
    users,
    pl.col("type") == pl.col("type_right")
)
shape: (4, 8)
| business        | type       | ... | user_name | budget |
| ---             | ---        | ... | ---       | ---    |
| str             | str        | ... | str       | i64    |
|-----------------|------------|-----|-----------|--------|
| 7burgers        | restaurant | ... | Alice     | 15     |
| Costa Coffee    | café       | ... | Bob       | 12     |
| Costa Coffee    | café       | ... | Bob       | 12     |
| The Queens Head | bar        | ... | Charlie   | 22     |
Data Transformation with Polars

複数条件で結合する

restaurants.join_where(
    users,
    pl.col("type") == pl.col("type_right"),
    pl.col("price") <= pl.col("budget")
)
shape: (3, 8)
| user_name | budget | business     | price | type       | ... |
| ---       | ---    | ---          | ---   | ---        | ... |
| str       | i64    | str          | i64   | str        | ... |
|-----------|--------|--------------|-------|------------|-----|
| Alice     | 15     | 7burgers     | 15    | restaurant | ... |
| Bob       | 12     | Costa Coffee | 8     | café       | ... |
| Bob       | 12     | Costa Coffee | 8     | café       | ... |
Data Transformation with Polars

最新の値で結合する

shape: (4, 3)
| business | date        | rating |
| ---      | ---         | ---    |
| str      | date        | i64    |
|----------|-------------|--------|
| 7burgers | 2025-03-15  | 5      |
| 7burgers | 2025-06-20  | 3      |
| 7burgers | 2025-09-10  | 4      |
| 7burgers | 2025-11-25  | 5      |

$$

  • タスク: 各レビューを最も関連する検査に対応付ける
shape: (3, 4)
| user   | business | date       | score |
| ---    | ---      | ---        | ---   |
| str    | str      | date       | f64   |
|--------|----------|------------|-------|
| Charlie | 7burgers | 2025-05-01 | 3.8   |
| Bob     | 7burgers | 2025-08-01 | 4.2   |
| Alice   | 7burgers | 2025-12-01 | 4.5   |
Data Transformation with Polars

最新の値で結合する

reviews.sort("date").join_asof(



)

$$

$$

$$

$$

  • join_asof - 特定日付時点で結合
Data Transformation with Polars

最新の値で結合する

reviews.sort("date").join_asof(
    inspections.sort("date"),


)
Data Transformation with Polars

最新の値で結合する

reviews.sort("date").join_asof(
    inspections.sort("date"),
    on="date",

)
Data Transformation with Polars

最新の値で結合する

reviews.sort("date").join_asof(
    inspections.sort("date"),
    on="date",
    strategy="backward"
)
| user    | date       | score | ... | rating |
| ---     | ---        | ---   | ... | ---    |
| str     | date       | f64   | ... | i64    |
|---------|------------|-------|-----|--------|
| Charlie | 2025-05-01 | 3.8   | ... | 5      |
| Bob     | 2025-08-01 | 4.2   | ... | 3      |
| Alice   | 2025-12-01 | 4.5   | ... | 5      |
Data Transformation with Polars

最も近い値で結合する

reviews.sort("date").join_asof(
    inspections.sort("date"),
    on="date",
    strategy="nearest"
)
| user    | date       | score | ... | rating |
| ---     | ---        | ---   | ... | ---    |
| str     | date       | f64   | ... | i64    |
|---------|------------|-------|-----|--------|
| Charlie | 2025-05-01 | 3.8   | ... | 5      |
| Bob     | 2025-08-01 | 4.2   | ... | 4      |
| Alice   | 2025-12-01 | 4.5   | ... | 5      |
Data Transformation with Polars

戦略の選択

$$

2つの時系列テーブル(各2行)の図

Data Transformation with Polars

戦略の選択 - backward

$$

左の2行を右の最初の行に対応付ける矢印がある、2つの時系列テーブルの図

Data Transformation with Polars

戦略の選択 - nearest

$$

左と右で各行が対応する矢印がある、2つの時系列テーブルの図

Data Transformation with Polars

戦略の選択 - forward

$$

左の2行を右の2番目の行に対応付ける矢印がある、2つの時系列テーブルの図

Data Transformation with Polars

グループ内での対応付け

reviews.sort("date").join_asof(
    restaurants.sort("date"),
    on="date",

by=["business", "location"]
)
| user    | business        | location    | date       | score | ... | rating |
| ---     | ---             | ---         | ---        | ---   | ... | ---    |
| str     | str             | str         | date       | f64   | ... | i64    |
|---------|-----------------|-------------|------------|-------|-----|--------|
| Charlie | 7burgers        | Wakey Wakey | 2025-05-01 | 3.8   | ... | null   |
| Bob     | Costa Coffee    | City Point  | 2025-05-01 | 4.0   | ... | 5      |
| Bob     | 7burgers        | Wakey Wakey | 2025-08-01 | 4.2   | ... | null   |
| Alice   | 7burgers        | Wakey Wakey | 2025-12-01 | 4.5   | ... | 4      |
| Alice   | The Queens Head | Denman St.  | 2025-12-01 | 4.7   | ... | 5      |
Data Transformation with Polars

練習してみましょう!

Data Transformation with Polars

Preparing Video For Download...