조건 또는 최근값으로 DataFrame 조인

Polars로 하는 데이터 변환

Liam Brannigan

Data Scientist & Polars Contributor

조건으로 조인하기

월드컵 우승국을 보여주는 단일 테이블

Polars로 하는 데이터 변환

조건으로 조인하기

월드컵 우승국 테이블과 은상/금상 최소 우승 수 테이블.

Polars로 하는 데이터 변환

조건으로 조인하기

월드컵 우승국 테이블, 레벨 테이블, 그리고 달성 레벨이 조인된 테이블

Polars로 하는 데이터 변환

조건으로 조인하기

월드컵 우승국 테이블, 레벨 테이블, 브라질 두 행이 강조된 조인 결과 테이블.

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        |
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로 하는 데이터 변환

가장 가까운 값으로 조인하기

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로 하는 데이터 변환

Vamos praticar!

Polars로 하는 데이터 변환

Preparing Video For Download...