カスタム変換を適用する

Data Transformation with 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 列を再スケール
Data Transformation with Polars

レビューを再スケール

def rescale_review(x):
    return 2 * x




Data Transformation with Polars

レビューを再スケール

def rescale_review(x):
    return 2 * x

venues.with_columns(
    pl.col("review")
)
Data Transformation with 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    |
Data Transformation with 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    |
Data Transformation with Polars

Polars の警告

PolarsInefficientMapWarning:

Expr.map_elements はネイティブ式 API より大幅に遅いです。
他に実装できない場合のみ使用してください。
この式を置き換え…
  - pl.col("review").map_elements(rescale_review)
次の式を使用:
  + 2 * pl.col("review")
Data Transformation with 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    |
Data Transformation with Polars

適切なツールを選ぶ

$$

ネイティブ式
  • 既定で推奨
  • 高速
  • 最適化済み

$$

.map_elements()
  • 必要時のみ
  • 可読性あり
  • サードパーティ活用可
Data Transformation with Polars

location テキストを標準化

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

$$

$$

$$

$$

$$

  • タスク: location 列の表記を標準化する
Data Transformation with Polars

location を標準化

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



)
Data Transformation with Polars

location を標準化

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


)
Data Transformation with 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  |
Data Transformation with 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
Data Transformation with 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  |
Data Transformation with Polars

カスタム式メソッドを追加

def standardize(input):





Data Transformation with Polars

カスタム式メソッドを追加

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

カスタム式メソッドを追加

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

pl.Expr.standardize = standardize
Data Transformation with 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  |
Data Transformation with Polars

Passons à la pratique !

Data Transformation with Polars

Preparing Video For Download...