열 변환

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 입문

dtype별 식

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 입문

Ayo berlatih!

Polars 입문

Preparing Video For Download...