การทำความสะอาดข้อมูลข้อความ

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

Liam Brannigan

Data Scientist & Polars Contributor

แนะนำผู้สอน

$$

$$

  • Liam Brannigan, Lead Data Scientist
  • ML and Data Engineering Specialist
  • Polars contributor

รูปโปรไฟล์ของผู้สอน

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

Transformation Engine

Animation

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

คอร์สนี้เหมาะกับคุณไหม?

$$

  • การสร้าง Polars DataFrame

$$

  • การใช้ Polars expression

$$

  • การทำ group-by aggregation

Introduction to Polars - หน้าคอร์ส

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

บทที่ 1

แผนภาพการทำความสะอาดข้อมูลข้อความ

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

บทที่ 2

ข้อมูลอนุกรมเวลา

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

บทที่ 3

การรวม DataFrame

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

บทที่ 4

เวิร์กโฟลว์แบบกำหนดเองและความสัมพันธ์

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

รู้จักชุดข้อมูลของเรา

import polars as pl

ratings = pl.read_csv("restaurant_ratings.csv")
shape: (5, 5)
| business         | location     | type       | rating | capacity |
| ---              | ---          | ---        | ---    | ---      |
| str              | str          | str        | f64    | f64      |
|------------------|--------------|------------|--------|----------|
|   7burgers       | Wakey Wakey  | restaurant | 5.0    | 55.0     |
| Bang Bang Burger | Forest Rd.   | restaurant | 4.0    | 55.0     |
| Costa Coffee     | City Point   | café       | 5.0    | 41.0     |
|  Costa Coffee    | The Moorgate | takeaway   | 5.0    | 0.0      |
| The Queens Head  | Denman St.   | bar        | 5.0    | 187.0    |
การแปลงข้อมูลด้วย Polars

แอปแนะนำร้านอาหาร

shape: (3, 5)
| business         | location     | type       | rating | capacity |
| ---              | ---          | ---        | ---    | ---      |
| str              | str          | str        | f64    | f64      |
|------------------|--------------|------------|--------|----------|
|   7burgers       | Wakey Wakey  | restaurant | 5.0    | 55.0     |
| Bang Bang Burger | Forest Rd.   | restaurant | 4.0    | 55.0     |
| Costa Coffee     | City Point   | café       | 5.0    | 41.0     |
  • ลบช่องว่าง
  • แปลงคอลัมน์ rating และ capacity
  • สร้างคอลัมน์ตัวระบุที่ไม่ซ้ำกัน
การแปลงข้อมูลด้วย Polars

การแปลง dtype ด้วย expression

ratings.with_columns(

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

การแปลง dtype ด้วย expression

ratings.with_columns(
    pl.col("rating").cast(pl.Int64)
)
shape: (5, 5)
| business         | location     | type       | rating | capacity |
| ---              | ---          | ---        | ---    | ---      |
| str              | str          | str        | i64    | f64      |
|------------------|--------------|------------|--------|----------|
|   7burgers       | Wakey Wakey  | restaurant | 5      | 55.0     |
| Bang Bang Burger | Forest Rd.   | restaurant | 4      | 55.0     |
| Costa Coffee     | City Point   | café       | 5      | 41.0     |
|  Costa Coffee    | The Moorgate | takeaway   | 5      | 0.0      |
| The Queens Head  | Denman St.   | bar        | 5      | 187.0    |
การแปลงข้อมูลด้วย Polars

การแปลงหลายคอลัมน์

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

การแปลงหลายคอลัมน์

ratings.cast({                    })
การแปลงข้อมูลด้วย Polars

การแปลงหลายคอลัมน์

ratings.cast({pl.Float64: pl.Int64})
shape: (5, 5)
| business         | location     | type       | rating | capacity |
| ---              | ---          | ---        | ---    | ---      |
| str              | str          | str        | i64    | i64      |
|------------------|--------------|------------|--------|----------|
|   7burgers       | Wakey Wakey  | restaurant | 5      | 55       |
| Bang Bang Burger | Forest Rd.   | restaurant | 4      | 55       |
| Costa Coffee     | City Point   | café       | 5      | 41       |
|  Costa Coffee    | The Moorgate | takeaway   | 5      | 0        |
| The Queens Head  | Denman St.   | bar        | 5      | 187      |
การแปลงข้อมูลด้วย Polars

การทำความสะอาดข้อมูลข้อความ

shape: (5, 5)
| business         | location        | type       | rating | capacity |
| ---              | ---             | ---        | ---    | ---      |
| str              | str             | str        | i64    | i64      |
|------------------|-----------------|------------|--------|----------|
|   7burgers       | Wakey Wakey     | restaurant | 5      | 55       |
| Bang Bang Burger | Forest Rd.      | restaurant | 4      | 55       |
| Costa Coffee     | City Point      | café       | 5      | 41       |
|  Costa Coffee    | The Moorgate    | takeaway   | 5      | 0        |
| The Queens Head  | Denman St.      | bar        | 5      | 187      |
การแปลงข้อมูลด้วย Polars

การทำความสะอาดข้อมูลข้อความ

  • .str.contains()
  • .str.strip_chars()
  • .str.strip_chars_start()
  • .str.to_lowercase()
  • ...
การแปลงข้อมูลด้วย Polars

การทำความสะอาดข้อมูลข้อความ

  • .str.contains()
  • .str.strip_chars()
  • .str.strip_chars_start()
  • .str.to_lowercase()
  • ...
1 https://docs.pola.rs/api/python/stable/reference/expressions/string.html
การแปลงข้อมูลด้วย Polars

การทำความสะอาดข้อมูลข้อความ

  • .str.contains()
  • .str.strip_chars()
  • .str.strip_chars_start()
  • .str.to_lowercase()
  • ...
1 https://docs.pola.rs/api/python/stable/reference/expressions/string.html
การแปลงข้อมูลด้วย Polars

การทำความสะอาดข้อมูลข้อความ

ratings.with_columns(

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

การทำความสะอาดข้อมูลข้อความ

ratings.with_columns(
    pl.col("business")
)
การแปลงข้อมูลด้วย Polars

การทำความสะอาดข้อมูลข้อความ

ratings.with_columns(
    pl.col("business").str.strip_chars_start()
)
shape: (5, 5)
| business         | location     | type       | rating | capacity |
| ---              | ---          | ---        | ---    | ---      |
| str              | str          | str        | i64    | i64      |
|------------------|--------------|------------|--------|----------|
| 7burgers         | Wakey Wakey  | restaurant | 5      | 55       |
| Bang Bang Burger | Forest Rd.   | restaurant | 4      | 55       |
| Costa Coffee     | City Point   | café       | 5      | 41       |
| Costa Coffee     | The Moorgate | takeaway   | 5      | 0        |
| The Queens Head  | Denman St.   | bar        | 5      | 187      |
การแปลงข้อมูลด้วย Polars

การรวมข้อมูลข้อความ

shape: (5, 5)
| business         | location        | type       | rating | capacity |
| ---              | ---             | ---        | ---    | ---      |
| str              | str             | str        | i64    | i64      |
|------------------|-----------------|------------|--------|----------|
| 7burgers         | Wakey Wakey     | restaurant | 5      | 55       |
| Bang Bang Burger | Forest Rd.      | restaurant | 4      | 55       |
| Costa Coffee     | City Point      | café       | 5      | 41       |
| Costa Coffee     | The Moorgate    | takeaway   | 5      | 0        |
| The Queens Head  | Denman St.      | bar        | 5      | 187      |
การแปลงข้อมูลด้วย Polars

การรวมข้อมูลข้อความ

ratings.with_columns(

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

การรวมข้อมูลข้อความ

ratings.with_columns(
    pl.concat_str(
)
การแปลงข้อมูลด้วย Polars

การรวมข้อมูลข้อความ

ratings.with_columns(
    pl.concat_str("business", "location"
)
การแปลงข้อมูลด้วย Polars

การรวมข้อมูลข้อความ

ratings.with_columns(
    pl.concat_str("business", "location", separator=":")
)
การแปลงข้อมูลด้วย Polars

การรวมข้อมูลข้อความ

ratings.with_columns(
    pl.concat_str("business", "location", separator=":").alias("id")
)
shape: (5, 5)
| business         | location     | type       | ... | id                            |
| ---              | ---          | ---        | --- | ---                           |
| str              | str          | str        | ... | str                           |
|------------------|--------------|------------|-----|-------------------------------|
| 7burgers         | Wakey Wakey  | restaurant | ... | 7burgers:Wakey Wakey          |
| Bang Bang Burger | Forest Rd.   | restaurant | ... | Bang Bang Burger:Forest Rd.   |
| Costa Coffee     | City Point   | café       | ... | Costa Coffee:City Point       |
| Costa Coffee     | The Moorgate | takeaway   | ... | Costa Coffee:The Moorgate     |
| The Queens Head  | Denman St.   | bar        | ... | The Queens Head:Denman St.    |
การแปลงข้อมูลด้วย Polars

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

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

Preparing Video For Download...