依條件或最近匹配合併 DataFrame

使用 Polars 進行資料轉換

Liam Brannigan

Data Scientist & Polars Contributor

依條件合併

單一資料表顯示世界盃冠軍

使用 Polars 進行資料轉換

依條件合併

一張顯示世界盃冠軍的表與一張顯示銀牌、金牌所需最少勝場的表。

使用 Polars 進行資料轉換

依條件合併

一張世界盃冠軍表、一張等級表,以及將冠軍對應到其達成等級的合併表

使用 Polars 進行資料轉換

依條件合併

一張世界盃冠軍表、一張等級表,以及合併後的表,並標示出巴西的兩列。

使用 Polars 進行資料轉換

有偏好的 App 使用者

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        |
使用 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        |
使用 Polars 進行資料轉換

依條件合併

restaurants.join_where(


)
使用 Polars 進行資料轉換

依條件合併

restaurants.join_where(
    users,

)
使用 Polars 進行資料轉換

依條件合併

restaurants.join_where(
    users,
    pl.col("type") == pl.col("type_right")
)
使用 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     |
使用 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é       | ... |
使用 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   |
使用 Polars 進行資料轉換

依最近值合併

reviews.sort("date").join_asof(



)

$$

$$

$$

$$

  • join_asof - 依指定日期進行對時合併
使用 Polars 進行資料轉換

依最近值合併

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


)
使用 Polars 進行資料轉換

依最近值合併

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

)
使用 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      |
使用 Polars 進行資料轉換

依最近值合併(nearest)

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      |
使用 Polars 進行資料轉換

選擇策略

$$

兩個時間序列資料表,各有兩列的示意圖。

使用 Polars 進行資料轉換

選擇策略-backward

$$

兩個時間序列資料表的示意圖,箭頭將左側兩列都對應到右側第一列。

使用 Polars 進行資料轉換

選擇策略-nearest

$$

兩個時間序列資料表的示意圖,箭頭將左側各列對應到右側相近的一列。

使用 Polars 進行資料轉換

選擇策略-forward

$$

兩個時間序列資料表的示意圖,箭頭將左側兩列都對應到右側第二列。

使用 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      |
使用 Polars 進行資料轉換

一起來練習吧!

使用 Polars 進行資料轉換

Preparing Video For Download...