Özel dönüşümleri uygulama

Polars ile Veri Dönüşümü

Liam Brannigan

Data Scientist & Polars Contributor

Mekanlar veri kümesi

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    |
  • Görev: review sütununu yeniden ölçeklemek
Polars ile Veri Dönüşümü

İncelemeleri yeniden ölçekle

def rescale_review(x):
    return 2 * x




Polars ile Veri Dönüşümü

İncelemeleri yeniden ölçekle

def rescale_review(x):
    return 2 * x

venues.with_columns(
    pl.col("review")
)
Polars ile Veri Dönüşümü

İncelemeleri yeniden ölçekle

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    |
Polars ile Veri Dönüşümü

Kontrol için return_dtype belirtin

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    |
Polars ile Veri Dönüşümü

Polars Uyarısı

PolarsInefficientMapWarning:

Expr.map_elements, yerel expressions API’sinden belirgin şekilde yavaştır.
Yalnızca mantığınızı başka türlü ASLA uygulayamazsanız kullanın.
Bu ifadeyi değiştirin...
  - pl.col("review").map_elements(rescale_review)
yerine şunu kullanın:
  + 2 * pl.col("review")
Polars ile Veri Dönüşümü

İncelemeleri yeniden ölçekle (yerel ifade)

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    |
Polars ile Veri Dönüşümü

Doğru aracı seçin

$$

Yerel ifadeler
  • Tercih edilen varsayılan
  • Hızlı
  • Optimize

$$

.map_elements()
  • Gerekliyse kullanın
  • Okunaklı
  • Üçüncü taraf paketler
Polars ile Veri Dönüşümü

Konum metnini standartlaştırma

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

$$

$$

$$

$$

$$

  • Görev: location sütununun biçimini standartlaştırmak
Polars ile Veri Dönüşümü

Konumları standartlaştırma

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



)
Polars ile Veri Dönüşümü

Konumları standartlaştırma

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


)
Polars ile Veri Dönüşümü

Konumları standartlaştırma

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  |
Polars ile Veri Dönüşümü

Bir ifadeyi değişkende saklama

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
Polars ile Veri Dönüşümü

İfadeyi yeniden kullanma

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  |
Polars ile Veri Dönüşümü

Özel bir ifade yöntemi ekleme

def standardize(input):





Polars ile Veri Dönüşümü

Özel bir ifade yöntemi ekleme

def standardize(input):
    return (
        input
        .str.replace_many(["St.", "Rd."], ["Street", "Road"])
        .str.to_uppercase()
    )
Polars ile Veri Dönüşümü

Özel bir ifade yöntemi ekleme

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

pl.Expr.standardize = standardize
Polars ile Veri Dönüşümü

Özel yöntemi kullanma

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  |
Polars ile Veri Dönüşümü

Hadi pratik yapalım!

Polars ile Veri Dönüşümü

Preparing Video For Download...