DataFrame 조인

Polars로 하는 데이터 변환

Liam Brannigan

Data Scientist & Polars Contributor

리뷰 데이터

shape: (5, 4)
| business         | location    | review | price |
| ---              | ---         | ---    | ---   |
| str              | str         | f64    | i64   |
|------------------|-------------|--------|-------|
| 7burgers         | Wakey Wakey | 4.2    | 15    |
| Bang Bang Burger | Forest Rd.  | 3.8    | 12    |
| Costa Coffee     | City Point  | 4.5    | 8     |
| Costa Coffee     | Waterloo    | 4.1    | 8     |
| The Queens Head  | Denman St.  | 4.7    | 25    |
Polars로 하는 데이터 변환

검사 데이터셋

inspections = pl.read_csv("restaurant_inspections.csv")
shape: (5, 4)
| business        | location    | type       | hygiene_rating |
| ---             | ---         | ---        | ---            |
| str             | str         | str        | i64            |
|-----------------|-------------|------------|----------------|
| 7burgers        | Wakey Wakey | restaurant | 4              |
| Costa Coffee    | City Point  | café       | 5              |
| Costa Coffee    | Waterloo    | café       | 3              |
| The Queens Head | Denman St.  | bar        | 5              |
| Wagamama        | Soho        | restaurant | 4              |
Polars로 하는 데이터 변환

조인이란?

단일 DataFrame을 보여주는 다이어그램.

Polars로 하는 데이터 변환

조인이란?

두 개의 DataFrame을 보여주는 다이어그램.

Polars로 하는 데이터 변환

조인이란?

일치하는 키가 강조된 두 DataFrame의 다이어그램.

Polars로 하는 데이터 변환

조인이란?

공유된 열을 기준으로 행을 매칭해 두 DataFrame을 결합하는 다이어그램.

Polars로 하는 데이터 변환

조인이란?

공유된 열로 행을 매칭하여 두 DataFrame을 결합하고, 일치하는 키가 강조된 다이어그램.

Polars로 하는 데이터 변환

단일 열로 조인

reviews.join(                                       )
Polars로 하는 데이터 변환

단일 열로 조인

reviews.join(inspections,                           )
Polars로 하는 데이터 변환

단일 열로 조인

reviews.join(inspections, on="business"             )
Polars로 하는 데이터 변환

단일 열로 조인

reviews.join(inspections, on="business", how="inner")
shape: (6, 7)
| business      | location    | ... | location_right | hygiene_rating |
| ---           | ---         | ... | ---            | ---            |
| str           | str         | ... | str            | i64            |
|---------------|-------------|-----|----------------|----------------|
| 7burgers      | Wakey Wakey | ... | Wakey Wakey    | 4              |
| Costa Coffee  | City Point  | ... | City Point     | 5              |
| Costa Coffee  | Waterloo    | ... | City Point     | 5              |
| Costa Coffee  | City Point  | ... | Waterloo       | 3              |
| Costa Coffee  | Waterloo    | ... | Waterloo       | 3              |
| The Queens Hd | Denman St.  | ... | Denman St.     | 5              |
Polars로 하는 데이터 변환

다중 열로 조인

reviews.join(inspections, on=["business", "location"])
Polars로 하는 데이터 변환

다중 열로 조인

reviews.join(inspections, on=["business", "location"])
shape: (4, 6)
| business        | location    | review | price | type       | hygiene_rating |
| ---             | ---         | ---    | ---   | ---        | ---            |
| str             | str         | f64    | i64   | str        | i64            |
|-----------------|-------------|--------|-------|------------|----------------|
| 7burgers        | Wakey Wakey | 4.2    | 15    | restaurant | 4              |
| Costa Coffee    | City Point  | 4.5    | 8     | café       | 5              |
| Costa Coffee    | Waterloo    | 4.1    | 8     | café       | 3              |
| The Queens Head | Denman St.  | 4.7    | 25    | bar        | 5              |
Polars로 하는 데이터 변환

Left 조인

reviews.join(inspections, on=["business", "location"],           )
Polars로 하는 데이터 변환

Left 조인

reviews.join(inspections, on=["business", "location"], how="left")
shape: (5, 6)
| business         | location    | review | price | type       | hygiene_rating |
| ---              | ---         | ---    | ---   | ---        | ---            |
| str              | str         | f64    | i64   | str        | i64            |
|------------------|-------------|--------|-------|------------|----------------|
| 7burgers         | Wakey Wakey | 4.2    | 15    | restaurant | 4              |
| Bang Bang Burger | Forest Rd.  | 3.8    | 12    | null       | null           |
| Costa Coffee     | City Point  | 4.5    | 8     | café       | 5              |
| Costa Coffee     | Waterloo    | 4.1    | 8     | café       | 3              |
| The Queens Head  | Denman St.  | 4.7    | 25    | bar        | 5              |
Polars로 하는 데이터 변환

Full 조인

reviews.join(inspections, on=["business", "location"],                          )
Polars로 하는 데이터 변환

Full 조인

reviews.join(inspections, on=["business", "location"], how="full"               )
Polars로 하는 데이터 변환

Full 조인

reviews.join(inspections, on=["business", "location"], how="full", coalesce=True)
shape: (6, 6)
| business         | location    | review | price | type       | hygiene_rating |
| ---              | ---         | ---    | ---   | ---        | ---            |
| str              | str         | f64    | i64   | str        | i64            |
|------------------|-------------|--------|-------|------------|----------------|
| 7burgers         | Wakey Wakey | 4.2    | 15    | restaurant | 4              |
| Costa Coffee     | City Point  | 4.5    | 8     | café       | 5              |
| Costa Coffee     | Waterloo    | 4.1    | 8     | café       | 3              |
| The Queens Head  | Denman St.  | 4.7    | 25    | bar        | 5              |
| Wagamama         | Soho        | null   | null  | restaurant | 4              |
| Bang Bang Burger | Forest Rd.  | 3.8    | 12    | null       | null           |
Polars로 하는 데이터 변환

조인 전략 선택

표 2개

Polars로 하는 데이터 변환

Inner 조인

Inner 조인 도식

Polars로 하는 데이터 변환

Left 조인

Left 조인 도식

Polars로 하는 데이터 변환

Full 조인

Full 조인 도식

Polars로 하는 데이터 변환

Ayo berlatih!

Polars로 하는 데이터 변환

Preparing Video For Download...