Kolommen transformeren

Introductie tot Polars

Liam Brannigan

Data Scientist & Polars Contributor

Kolommen transformeren

$$

  • Data uit de praktijk moet worden opgeschoond
  • Polars-expressies

$$

Doel: data klaarmaken voor een kortingscampagne

Illustratie van een bestand en een bezem om opschonen en transformeren te symboliseren

Introductie tot Polars

Expressies maken: pl.col()

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

Rekenen met expressies

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  |
| ...          | ...    |
Introductie tot Polars

Transformaties ketenen

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

Afbeelding van een ketting.

Introductie tot Polars

Een expressie hernoemen

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            |
| ...       | ...              |
Introductie tot Polars

Expressies maken van een constante

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      |
| ...          | ...              | ...       |
Introductie tot Polars

Parallelle verwerking

$$

$$

Afbeelding met drie taken die parallel worden uitgevoerd

Seriële verwerking

$$

$$

$$

Afbeelding met drie taken die opeenvolgend worden uitgevoerd

Introductie tot Polars

Een kolom aggregeren

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      |
Introductie tot Polars

Ingebouwde transformaties

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       |
| ...         | ...   | ...        |
Introductie tot Polars

Dtype-specifieke expressies

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 |
| ...         | ...     |
Introductie tot Polars

Laten we oefenen!

Introductie tot Polars

Preparing Video For Download...