Biến đổi cột

Nhập môn Polars

Liam Brannigan

Data Scientist & Polars Contributor

Biến đổi cột

$$

  • Dữ liệu thực tế cần làm sạch
  • Polar Expressions

$$

Mục tiêu: chuẩn bị dữ liệu cho chiến dịch giảm giá

Minh họa tệp và chổi tượng trưng cho làm sạch và biến đổi dữ liệu

Nhập môn Polars

Tạo biểu thức: pl.col()

rentals.select(
  "name", 
  pl.col("price")
)
shape: (49, 2)
| name         | price |
| ---          | ---   |
| str          | i64   |
|--------------|-------|
| Waves        | 540   |
| Seashells    | 540   |
| Lake view    | 714   |
| ...          | ...   |
Nhập môn Polars

Tính toán với biểu thức

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  |
| ...          | ...    |
Nhập môn Polars

Chuỗi biến đổi

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

Hình một sợi xích.

Nhập môn Polars

Đổi tên một biểu thức

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            |
| ...       | ...              |
Nhập môn Polars

Tạo biểu thức từ hằng số

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      |
| ...          | ...              | ...       |
Nhập môn Polars

Xử lý song song

$$

$$

Hình minh họa ba tác vụ được thực hiện song song

Xử lý tuần tự

$$

$$

$$

Hình minh họa ba tác vụ được thực hiện tuần tự

Nhập môn Polars

Gộp (aggregate) một cột

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      |
Nhập môn Polars

Biến đổi dựng sẵn

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       |
| ...         | ...   | ...        |
Nhập môn Polars

Biểu thức theo kiểu dữ liệu

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 |
| ...         | ...     |
Nhập môn Polars

Ayo berlatih!

Nhập môn Polars

Preparing Video For Download...