套用自訂轉換

使用 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 進行資料轉換

重新縮放評分

def rescale_review(x):
    return 2 * x




使用 Polars 進行資料轉換

重新縮放評分

def rescale_review(x):
    return 2 * x

venues.with_columns(
    pl.col("review")
)
使用 Polars 進行資料轉換

重新縮放評分

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 進行資料轉換

重新縮放評分(原生表示式)

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 進行資料轉換

選對工具

$$

原生表示式
  • 預設優先
  • 快速
  • 已最佳化

$$

.map_elements()
  • 需要時再用
  • 可讀性佳
  • 可用第三方套件
使用 Polars 進行資料轉換

標準化地點文字

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

$$

$$

$$

$$

$$

  • 任務:標準化 location 欄位的格式
使用 Polars 進行資料轉換

標準化地點

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



)
使用 Polars 進行資料轉換

標準化地點

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


)
使用 Polars 進行資料轉換

標準化地點

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 進行資料轉換

將表示式存入變數

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 進行資料轉換

重複使用表示式

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 進行資料轉換

加入自訂表示式方法

def standardize(input):





使用 Polars 進行資料轉換

加入自訂表示式方法

def standardize(input):
    return (
        input
        .str.replace_many(["St.", "Rd."], ["Street", "Road"])
        .str.to_uppercase()
    )
使用 Polars 進行資料轉換

加入自訂表示式方法

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

pl.Expr.standardize = standardize
使用 Polars 進行資料轉換

使用自訂方法

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...