การเชื่อมต่อ DataFrame

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

Liam Brannigan

Data Scientist & Polars Contributor

กลับมาที่แอปร้านอาหาร

shape: (4, 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     |
| The Queens Head  | Denman St.  | 4.7    | 25    |
การแปลงข้อมูลด้วย Polars

การเชื่อมต่อข้อมูล

ไดอะแกรมแสดง DataFrame สามชุดเรียงในแนวตั้ง

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

การเชื่อมต่อข้อมูล

ไดอะแกรมแสดง DataFrame สามชุดเรียงในแนวตั้งรวมเป็น DataFrame เดียว

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

ข้อมูลร้านอาหารย่านใจกลางลอนดอน

import polars as pl

central = pl.read_csv("restaurants_central.csv")
shape: (4, 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     |
| The Queens Head  | Denman St.  | 4.7    | 25    |
การแปลงข้อมูลด้วย Polars

ข้อมูลร้านอาหารย่านลอนดอนใต้

south = pl.read_csv("restaurants_south.csv")
shape: (3, 4)
| business       | location       | review | price |
| ---            | ---            | ---    | ---   |
| str            | str            | f64    | i64   |
|----------------|----------------|--------|-------|
| Pizzeria Bella | Bermondsey St. | 3.5    | 18    |
| Costa Coffee   | Waterloo       | 4.1    | 8     |
| Nando's        | Brixton Rd.    | 4.0    | 20    |

$$

  • การเชื่อมต่อแนวตั้ง - นำ DataFrame มาวางซ้อนกัน
การแปลงข้อมูลด้วย Polars

การเชื่อมต่อแนวตั้ง

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

การเชื่อมต่อแนวตั้ง

reviews = pl.concat([central, south]                )
การแปลงข้อมูลด้วย Polars

การเชื่อมต่อแนวตั้ง

reviews = pl.concat([central, south], how="vertical")
shape: (7, 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     |
| The Queens Head  | Denman St.     | 4.7    | 25    |
| Pizzeria Bella   | Bermondsey St. | 3.5    | 18    |
| Costa Coffee     | Waterloo       | 4.1    | 8     |
| Nando's          | Brixton Rd.    | 4.0    | 20    |
การแปลงข้อมูลด้วย Polars

การเชื่อมต่อแนวตั้ง

reviews = pl.read_csv("restaurants*.csv")
shape: (7, 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     |
| The Queens Head  | Denman St.     | 4.7    | 25    |
| Pizzeria Bella   | Bermondsey St. | 3.5    | 18    |
| Costa Coffee     | Waterloo       | 4.1    | 8     |
| Nando's          | Brixton Rd.    | 4.0    | 20    |
การแปลงข้อมูลด้วย Polars

ชุดข้อมูลที่สามซึ่งมีข้อมูลขาดหาย

north = pl.read_csv("north.csv")
shape: (2, 3)
| business      | location      | review |
| ---           | ---           | ---    |
| str           | str           | f64    |
|-----------    |---------------|--------|
| Pig & Butcher | Liverpool Rd. | 3.2    |
| The Castle    | Angle         | 4.4    |
การแปลงข้อมูลด้วย Polars

การเชื่อมต่อแนวตั้งล้มเหลว

pl.concat([central, south, north], how="vertical")
ShapeError: unable to append to a DataFrame of width 4 with a DataFrame of width 3
การแปลงข้อมูลด้วย Polars

การเชื่อมต่อแบบทแยงมุม

pl.concat([central, south, north], how="diagonal")
การแปลงข้อมูลด้วย Polars

การเชื่อมต่อแบบทแยงมุม

pl.concat([central, south, north], how="diagonal")
shape: (9, 4)
| business         | location            | review | price |
| ---              | ---                 | ---    | ---   |
| str              | str                 | f64    | i64   |
|------------------|---------------------|--------|-------|
| 7burgers         | Wakey Wakey         | 4.2    | 15    |
| ...              | ...                 | ...    | ...   |
| Nando's          | Brixton Rd.         | 4.0    | 20    |
| Pig & Butcher    | Liverpool Rd.       | 3.2    | null  |
| The Castle       | Angle               | 4.4    | null  |
การแปลงข้อมูลด้วย Polars

การเชื่อมต่อแนวนอน

cuisine = pl.read_csv("cuisine.csv")
shape: (7, 1)
| cuisine  |
| ---      |
| str      |
|----------|
| burgers  |
| burgers  |
| coffee   |
| ...      |
| chicken  |
  • การเชื่อมต่อแนวนอน - นำ DataFrame มาวางเคียงกัน
การแปลงข้อมูลด้วย Polars

การเชื่อมต่อแนวนอน

pl.concat([reviews, cuisine], how="horizontal")
shape: (7, 5)
| business         | location       | review | price | cuisine  |
| ---              | ---            | ---    | ---   | ---      |
| str              | str            | f64    | i64   | str      |
|------------------|----------------|--------|-------|----------|
| 7burgers         | Wakey Wakey    | 4.2    | 15    | burgers  |
| Bang Bang Burger | Forest Rd.     | 3.8    | 12    | burgers  |
| Costa Coffee     | City Point     | 4.5    | 8     | coffee   |
| The Queens Head  | Denman St.     | 4.7    | 25    | pub_food |
| Pizzeria Bella   | Bermondsey St. | 3.5    | 18    | pizza    |
| Costa Coffee     | Waterloo       | 4.1    | 8     | coffee   |
| Nando's          | Brixton Rd.    | 4.0    | 20    | chicken  |
การแปลงข้อมูลด้วย Polars

การต่อท้ายข้อมูลด้วย extend

new_listing = pl.DataFrame({
    "business": ["Wagamama"],
    "location": ["Soho"],
    "review": [4.3],
    "price": [16]
})
shape: (1, 4)
| business         | location     | review | price |
| ---              | ---          | ---    | ---   |
| str              | str          | f64    | i64   |
|------------------|--------------|--------|-------|
| Wagamama         | Soho         | 4.3    | 16    |
การแปลงข้อมูลด้วย Polars

การต่อท้ายข้อมูลด้วย extend

reviews.extend(new_listing)
shape: (8, 4)
| business         | location       | review | price |
| ---              | ---            | ---    | ---   |
| str              | str            | f64    | i64   |
|------------------|----------------|--------|-------|
| 7burgers         | Wakey Wakey    | 4.2    | 15    |
| ...              | ...            | ...    | ...   |
| Nando's          | Brixton Rd.    | 4.0    | 20    |
| Wagamama         | Soho           | 4.3    | 16    |
การแปลงข้อมูลด้วย Polars

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

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

Preparing Video For Download...