Applicare trasformazioni personalizzate

Trasformazione dei dati con Polars

Liam Brannigan

Data Scientist & Polars Contributor

Dataset delle sedi

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    |
  • Obiettivo: riscalare la colonna review
Trasformazione dei dati con Polars

Riscalare le recensioni

def rescale_review(x):
    return 2 * x




Trasformazione dei dati con Polars

Riscalare le recensioni

def rescale_review(x):
    return 2 * x

venues.with_columns(
    pl.col("review")
)
Trasformazione dei dati con Polars

Riscalare le recensioni

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    |
Trasformazione dei dati con Polars

Specificare return_dtype per il controllo

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    |
Trasformazione dei dati con Polars

Avviso di Polars

PolarsInefficientMapWarning:

Expr.map_elements è significativamente più lento dell'API di espressioni nativa.
Usalo solo se NON puoi implementare la logica in altro modo.
Sostituisci questa espressione...
  - pl.col("review").map_elements(rescale_review)
con questa:
  + 2 * pl.col("review")
Trasformazione dei dati con Polars

Riscalare le recensioni (espressione nativa)

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    |
Trasformazione dei dati con Polars

Scegli lo strumento giusto

$$

Espressioni native
  • Scelta predefinita
  • Veloci
  • Ottimizzate

$$

.map_elements()
  • Usa quando serve
  • Leggibile
  • Pacchetti di terze parti
Trasformazione dei dati con Polars

Standardizzare il testo delle location

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

$$

$$

$$

$$

$$

  • Obiettivo: standardizzare il formato della colonna location
Trasformazione dei dati con Polars

Standardizzare le location

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



)
Trasformazione dei dati con Polars

Standardizzare le location

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


)
Trasformazione dei dati con Polars

Standardizzare le location

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  |
Trasformazione dei dati con Polars

Salvare un'espressione in una variabile

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
Trasformazione dei dati con Polars

Riutilizzare l'espressione

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  |
Trasformazione dei dati con Polars

Aggiungere un metodo di espressione personalizzato

def standardize(input):





Trasformazione dei dati con Polars

Aggiungere un metodo di espressione personalizzato

def standardize(input):
    return (
        input
        .str.replace_many(["St.", "Rd."], ["Street", "Road"])
        .str.to_uppercase()
    )
Trasformazione dei dati con Polars

Aggiungere un metodo di espressione personalizzato

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

pl.Expr.standardize = standardize
Trasformazione dei dati con Polars

Usare il metodo personalizzato

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  |
Trasformazione dei dati con Polars

Passons à la pratique !

Trasformazione dei dati con Polars

Preparing Video For Download...