การ Join 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

Join คืออะไร?

ไดอะแกรมแสดง DataFrame เดียว

การแปลงข้อมูลด้วย Polars

Join คืออะไร?

ไดอะแกรมแสดง DataFrame สอง DataFrame

การแปลงข้อมูลด้วย Polars

Join คืออะไร?

ไดอะแกรมแสดง DataFrame สอง DataFrame พร้อมไฮไลต์คีย์ที่ตรงกัน

การแปลงข้อมูลด้วย Polars

Join คืออะไร?

ไดอะแกรมแสดงการรวม DataFrame สอง DataFrame โดยจับคู่แถวบนคอลัมน์ที่ใช้ร่วมกัน

การแปลงข้อมูลด้วย Polars

Join คืออะไร?

ไดอะแกรมแสดงการรวม DataFrame สอง DataFrame โดยจับคู่แถวบนคอลัมน์ที่ใช้ร่วมกัน พร้อมไฮไลต์คีย์ที่ตรงกัน

การแปลงข้อมูลด้วย Polars

Join บนคอลัมน์เดียว

reviews.join(                                       )
การแปลงข้อมูลด้วย Polars

Join บนคอลัมน์เดียว

reviews.join(inspections,                           )
การแปลงข้อมูลด้วย Polars

Join บนคอลัมน์เดียว

reviews.join(inspections, on="business"             )
การแปลงข้อมูลด้วย Polars

Join บนคอลัมน์เดียว

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

Join บนหลายคอลัมน์

reviews.join(inspections, on=["business", "location"])
การแปลงข้อมูลด้วย Polars

Join บนหลายคอลัมน์

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 join

reviews.join(inspections, on=["business", "location"],           )
การแปลงข้อมูลด้วย Polars

Left join

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 join

reviews.join(inspections, on=["business", "location"],                          )
การแปลงข้อมูลด้วย Polars

Full join

reviews.join(inspections, on=["business", "location"], how="full"               )
การแปลงข้อมูลด้วย Polars

Full join

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

การเลือกกลยุทธ์การ Join

2 ตาราง

การแปลงข้อมูลด้วย Polars

Inner join

การแสดง Inner join

การแปลงข้อมูลด้วย Polars

Left join

การแสดง Left join

การแปลงข้อมูลด้วย Polars

Full join

การแสดง Full join

การแปลงข้อมูลด้วย Polars

มาฝึกกันเถอะ!

การแปลงข้อมูลด้วย Polars

Preparing Video For Download...