การประยุกต์ใช้การแปลงข้อมูลแบบกำหนดเอง

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

Liam Brannigan

Data Scientist & Polars Contributor

ชุดข้อมูล venues

venues = pl.read_csv("venues.csv")
shape: (4, 6)
| business         | location    | type       | hygiene_rating | review | price |
| ---              | ---         | ---        | ---            | ---    | ---   |
| str              | str         | str        | i64            | f64    | i64   |
|------------------|-------------|------------|----------------|--------|-------|
| 7burgers         | Wakey Wakey | restaurant | 4              | 4.2    | 15    |
| Bang Bang Burger | Forest Rd.  | restaurant | 3              | 3.8    | 12    |
| Costa Coffee     | City Point  | café       | 5              | 4.5    | 8     |
| The Queens Head  | Denman St.  | bar        | 5              | 4.7    | 25    |
  • งาน: ปรับสเกลคอลัมน์ review
การแปลงข้อมูลด้วย Polars

ปรับสเกล review

def rescale_review(x):
    return 2 * x




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

ปรับสเกล review

def rescale_review(x):
    return 2 * x

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

ปรับสเกล review

def rescale_review(x):
    return 2 * x

venues.with_columns(
    pl.col("review").map_elements(rescale_review)
)
shape: (4, 6)
| business         | location    | type       | hygiene_rating | review | price |
| ---              | ---         | ---        | ---            | ---    | ---   |
| str              | str         | str        | i64            | f64    | i64   |
|------------------|-------------|------------|----------------|--------|-------|
| 7burgers         | Wakey Wakey | restaurant | 4              | 8.4    | 15    |
| Bang Bang Burger | Forest Rd.  | restaurant | 3              | 7.6    | 12    |
| Costa Coffee     | City Point  | café       | 5              | 9.0    | 8     |
| The Queens Head  | Denman St.  | bar        | 5              | 9.4    | 25    |
การแปลงข้อมูลด้วย Polars

ระบุ return_dtype เพื่อควบคุมประเภทข้อมูล

venues.with_columns(
    pl.col("review").map_elements(
        rescale_review, return_dtype=pl.Float64
    )
)
shape: (4, 6)
| business         | location    | type       | hygiene_rating | review | price |
| ---              | ---         | ---        | ---            | ---    | ---   |
| str              | str         | str        | i64            | f64    | i64   |
|------------------|-------------|------------|----------------|--------|-------|
| 7burgers         | Wakey Wakey | restaurant | 4              | 8.4    | 15    |
| Bang Bang Burger | Forest Rd.  | restaurant | 3              | 7.6    | 12    |
| Costa Coffee     | City Point  | café       | 5              | 9.0    | 8     |
| The Queens Head  | Denman St.  | bar        | 5              | 9.4    | 25    |
การแปลงข้อมูลด้วย Polars

คำเตือนจาก Polars

PolarsInefficientMapWarning:

Expr.map_elements is significantly slower than the native expressions API.
Only use if you absolutely CANNOT implement your logic otherwise.
Replace this expression...
  - pl.col("review").map_elements(rescale_review)
with this one instead:
  + 2 * pl.col("review")
การแปลงข้อมูลด้วย Polars

ปรับสเกล review (native expression)

venues.with_columns(
    (2 * pl.col("review")).alias("review")
)
shape: (4, 6)
| business         | location    | type       | hygiene_rating | review | price |
| ---              | ---         | ---        | ---            | ---    | ---   |
| str              | str         | str        | i64            | f64    | i64   |
|------------------|-------------|------------|----------------|--------|-------|
| 7burgers         | Wakey Wakey | restaurant | 4              | 8.4    | 15    |
| Bang Bang Burger | Forest Rd.  | restaurant | 3              | 7.6    | 12    |
| Costa Coffee     | City Point  | café       | 5              | 9.0    | 8     |
| The Queens Head  | Denman St.  | bar        | 5              | 9.4    | 25    |
การแปลงข้อมูลด้วย Polars

เลือกใช้เครื่องมือให้เหมาะสม

$$

Native expressions
  • ตัวเลือกหลักที่แนะนำ
  • รวดเร็ว
  • ผ่านการปรับให้เหมาะสมแล้ว

$$

.map_elements()
  • ใช้เมื่อจำเป็น
  • อ่านง่าย
  • รองรับแพ็กเกจของบุคคลที่สาม
การแปลงข้อมูลด้วย Polars

ทำให้ข้อความ location เป็นมาตรฐาน

  • Denman St. -> DENMAN STREET
  • Forest Rd. -> FOREST ROAD

$$

$$

$$

$$

$$

  • งาน: ทำให้รูปแบบของคอลัมน์ location เป็นมาตรฐาน
การแปลงข้อมูลด้วย Polars

ทำให้ location เป็นมาตรฐาน

venues.with_columns(
    pl.col("location")



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

ทำให้ location เป็นมาตรฐาน

venues.with_columns(
    pl.col("location")
    .str.replace_many(["St.", "Rd."], ["Street", "Road"])


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

ทำให้ location เป็นมาตรฐาน

venues.with_columns(
    pl.col("location")
    .str.replace_many(["St.", "Rd."], ["Street", "Road"])
    .str.to_uppercase()
    .alias("location_clean")
)
shape: (4, 7)
| business         | location    | ... | location_clean |
| ---              | ---         | --- | ---            |
| str              | str         | ... | str            |
|------------------|-------------|-----|----------------|
| 7burgers         | Wakey Wakey | ... | WAKEY WAKEY    |
| Bang Bang Burger | Forest Rd.  | ... | FOREST ROAD    |
| Costa Coffee     | City Point  | ... | CITY POINT     |
| The Queens Head  | Denman St.  | ... | DENMAN STREET  |
การแปลงข้อมูลด้วย Polars

เก็บ expression ไว้ในตัวแปร

standardize_locations_expr = (
    pl.col("location")
    .str.replace_many(["St.", "Rd."], ["Street", "Road"])
    .str.to_uppercase()
    .alias("location_clean")
)

type(standardize_locations_expr)
polars.Expr
การแปลงข้อมูลด้วย Polars

นำ expression กลับมาใช้ซ้ำ

venues.with_columns(
    standardize_locations_expr
)
shape: (4, 7)
| business         | location    | ... | location_clean |
| ---              | ---         | --- | ---            |
| str              | str         | ... | str            |
|------------------|-------------|-----|----------------|
| 7burgers         | Wakey Wakey | ... | WAKEY WAKEY    |
| Bang Bang Burger | Forest Rd.  | ... | FOREST ROAD    |
| Costa Coffee     | City Point  | ... | CITY POINT     |
| The Queens Head  | Denman St.  | ... | DENMAN STREET  |
การแปลงข้อมูลด้วย Polars

เพิ่ม expression method แบบกำหนดเอง

def standardize(input):





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

เพิ่ม expression method แบบกำหนดเอง

def standardize(input):
    return (
        input
        .str.replace_many(["St.", "Rd."], ["Street", "Road"])
        .str.to_uppercase()
    )
การแปลงข้อมูลด้วย Polars

เพิ่ม expression method แบบกำหนดเอง

def standardize(input):
    return (
        input
        .str.replace_many(["St.", "Rd."], ["Street", "Road"])
        .str.to_uppercase()
    )

pl.Expr.standardize = standardize
การแปลงข้อมูลด้วย Polars

ใช้ method แบบกำหนดเอง

restaurants.with_columns(
    pl.col("address").standardize().alias("address_clean")
)
shape: (4, 7)
| business         | address     | ... | address_clean  |
| ---              | ---         | --- | ---            |
| str              | str         | ... | str            |
|------------------|-------------|-----|----------------|
| 7burgers         | Wakey Wakey | ... | WAKEY WAKEY    |
| Bang Bang Burger | Forest Rd.  | ... | FOREST ROAD    |
| Costa Coffee     | City Point  | ... | CITY POINT     |
| The Queens Head  | Denman St.  | ... | DENMAN STREET  |
การแปลงข้อมูลด้วย Polars

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

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

Preparing Video For Download...