轉換欄位

Polars 入門

Liam Brannigan

Data Scientist & Polars Contributor

轉換欄位

$$

  • 真實資料需要清理
  • Polar Expressions(運算式)

$$

目標:為折扣行銷活動準備資料

以檔案和掃帚象徵資料清理與轉換的插圖

Polars 入門

建立運算式:pl.col()

rentals.select(
  "name", 
  pl.col("price")
)
shape: (49, 2)
| name         | price |
| ---          | ---   |
| str          | i64   |
|--------------|-------|
| Waves        | 540   |
| Seashells    | 540   |
| Lake view    | 714   |
| ...          | ...   |
Polars 入門

以運算式進行算術運算

rentals.select(
  "name",
  pl.col("price") * 0.8
)
shape: (49, 3)
| name         | price  |
| ---          | ---    |
| str          | f64    |
|--------------|--------|
| Waves        | 432.0  |
| Seashells    | 432.0  |
| Lake view    | 571.2  |
| ...          | ...    |
Polars 入門

串接轉換

rentals.select(
  "name",
  (pl.col("price") * 0.8).round(0)
)
shape: (49, 2)
| name         | price  |
| ---          | ---    |
| str          | f64    |
|--------------|--------|
| Waves        | 432.0  |
| Seashells    | 432.0  |
| Lake view    | 571.0  |
| ...          | ...    |

鍊條圖片。

Polars 入門

重新命名運算式

rentals.select(
  "name", 
  (pl.col("price") * 0.8).round(0).alias("discounted_price") 
)
shape: (49, 2)
| name      | discounted_price |
| ---       | ---              |
| str       | f64              |
|-----------|------------------|
| Waves     | 432.0            |
| Seashells | 432.0            |
| Lake view | 571.0            |
| ...       | ...              |
Polars 入門

由常數建立運算式

rentals.select(
  "name", 
  (pl.col("price") * 0.8).round(0).alias("discounted_price"),
  pl.lit(True).alias("available")
)
shape: (49, 3)
| name         | discounted_price | available |
| ---          | ---              | ---       |
| str          | f64              | bool      |
|--------------|------------------|-----------|
| Waves        | 432.0            | true      |
| Seashells    | 432.0            | true      |
| Lake view    | 571.0            | true      |
| ...          | ...              | ...       |
Polars 入門

平行處理

$$

$$

示意三個任務同時平行執行的圖片

串行處理

$$

$$

$$

示意三個任務依序執行的圖片

Polars 入門

彙總欄位

rentals.select(
   "name", 
   "price",
  pl.col("price").mean().alias("avg_price"),
  pl.col("price").max().alias("max_price")
)
shape: (49, 4)
| name                | price | avg_price | max_price |
| ---                 | ---   | ---       | ---       |
| str                 | i64   | f64       | i64       |
|---------------------|-------|-----------|-----------|
| Waves               | 540   | 979.6     | 2411      |
| Seashells           | 540   | 979.6     | 2411      |
| Lake view           | 714   | 979.6     | 2411      |
Polars 入門

內建轉換

rentals.select(
   "name",
   "price",
   pl.col("price").rank().alias("rank_price")
)
shape: (49, 3)
| name        | price | rank_price |
| ---         | ---   | ---        |
| str         | i64   | f64        |
|-------------|-------|------------|
| Waves       | 540   | 7.5        |
| Seashells   | 540   | 7.5        |
| Lake view   | 714   | 19.0       |
| ...         | ...   | ...        |
Polars 入門

依資料型別的運算式

rentals.select(
  "name",
  "type"
)
shape: (49, 2)
| name        | type    |
| ---         | ---     |
| str         | str     |
|-------------|---------|
| Waves       | Cottage |
| Seashells   | Cottage |
| ...         | ...     |
rentals.select(
  "name",
  pl.col("type").str.to_lowercase()
)
shape: (49, 2)
| name        | type    |
| ---         | ---     |
| str         | str     |
|-------------|---------|
| Waves       | cottage |
| Seashells   | cottage |
| ...         | ...     |
Polars 入門

一起來練習吧!

Polars 入門

Preparing Video For Download...