DataFrame の結合

Data Transformation with 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    |
Data Transformation with 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              |
Data Transformation with Polars

結合とは?

単一のDataFrameを示す図。

Data Transformation with Polars

結合とは?

2つのDataFrameを示す図。

Data Transformation with Polars

結合とは?

一致キーを強調した2つのDataFrameの図。

Data Transformation with Polars

結合とは?

共有列で行を一致させて2つのDataFrameを結合する図。

Data Transformation with Polars

結合とは?

共有列の一致キーを強調し、対応する行で2つのDataFrameを結合する図。

Data Transformation with Polars

単一列での結合

reviews.join(                                       )
Data Transformation with Polars

単一列での結合

reviews.join(inspections,                           )
Data Transformation with Polars

単一列での結合

reviews.join(inspections, on="business"             )
Data Transformation with 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              |
Data Transformation with Polars

複数列での結合

reviews.join(inspections, on=["business", "location"])
Data Transformation with 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              |
Data Transformation with Polars

左結合

reviews.join(inspections, on=["business", "location"],           )
Data Transformation with Polars

左結合

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              |
Data Transformation with Polars

完全結合

reviews.join(inspections, on=["business", "location"],                          )
Data Transformation with Polars

完全結合

reviews.join(inspections, on=["business", "location"], how="full"               )
Data Transformation with Polars

完全結合

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           |
Data Transformation with Polars

結合戦略を選ぶ

2つのテーブル

Data Transformation with Polars

内部結合

内部結合の図示

Data Transformation with Polars

左結合

左結合の図示

Data Transformation with Polars

完全結合

完全結合の図示

Data Transformation with Polars

演習に進みましょう!

Data Transformation with Polars

Preparing Video For Download...