Pulizia dei dati testuali

Trasformazione dei dati con Polars

Liam Brannigan

Data Scientist & Polars Contributor

Conosci l'istruttore

$$

$$

  • Liam Brannigan, Lead Data Scientist
  • Specialista ML e Data Engineering
  • Contributor di Polars

Foto profilo dell'istruttore.

Trasformazione dei dati con Polars

Motore di trasformazione

Animazione

Trasformazione dei dati con Polars

Questo corso fa per te?

$$

  • Creare un DataFrame Polars

$$

  • Usare un'espressione Polars

$$

  • Eseguire un group-by con aggregazione

Introduzione a Polars - pagina del corso

Trasformazione dei dati con Polars

Capitolo 1

Diagramma di pulizia dei dati testuali

Trasformazione dei dati con Polars

Capitolo 2

Dati di serie temporali

Trasformazione dei dati con Polars

Capitolo 3

Unire DataFrame

Trasformazione dei dati con Polars

Capitolo 4

Workflow personalizzati e correlazione

Trasformazione dei dati con Polars

Conosci il dataset

import polars as pl

ratings = pl.read_csv("restaurant_ratings.csv")
shape: (5, 5)
| business         | location     | type       | rating | capacity |
| ---              | ---          | ---        | ---    | ---      |
| str              | str          | str        | f64    | f64      |
|------------------|--------------|------------|--------|----------|
|   7burgers       | Wakey Wakey  | restaurant | 5.0    | 55.0     |
| Bang Bang Burger | Forest Rd.   | restaurant | 4.0    | 55.0     |
| Costa Coffee     | City Point   | café       | 5.0    | 41.0     |
|  Costa Coffee    | The Moorgate | takeaway   | 5.0    | 0.0      |
| The Queens Head  | Denman St.   | bar        | 5.0    | 187.0    |
Trasformazione dei dati con Polars

App di raccomandazione ristoranti

shape: (3, 5)
| business         | location     | type       | rating | capacity |
| ---              | ---          | ---        | ---    | ---      |
| str              | str          | str        | f64    | f64      |
|------------------|--------------|------------|--------|----------|
|   7burgers       | Wakey Wakey  | restaurant | 5.0    | 55.0     |
| Bang Bang Burger | Forest Rd.   | restaurant | 4.0    | 55.0     |
| Costa Coffee     | City Point   | café       | 5.0    | 41.0     |
  • Rimuovi spazi bianchi
  • Converti le colonne rating e capacity
  • Crea una colonna identificatore univoco
Trasformazione dei dati con Polars

Cast del dtype con un'espressione

ratings.with_columns(

)
Trasformazione dei dati con Polars

Cast del dtype con un'espressione

ratings.with_columns(
    pl.col("rating").cast(pl.Int64)
)
shape: (5, 5)
| business         | location     | type       | rating | capacity |
| ---              | ---          | ---        | ---    | ---      |
| str              | str          | str        | i64    | f64      |
|------------------|--------------|------------|--------|----------|
|   7burgers       | Wakey Wakey  | restaurant | 5      | 55.0     |
| Bang Bang Burger | Forest Rd.   | restaurant | 4      | 55.0     |
| Costa Coffee     | City Point   | café       | 5      | 41.0     |
|  Costa Coffee    | The Moorgate | takeaway   | 5      | 0.0      |
| The Queens Head  | Denman St.   | bar        | 5      | 187.0    |
Trasformazione dei dati con Polars

Cast di più colonne

ratings.cast(                      )
Trasformazione dei dati con Polars

Cast di più colonne

ratings.cast({                    })
Trasformazione dei dati con Polars

Cast di più colonne

ratings.cast({pl.Float64: pl.Int64})
shape: (5, 5)
| business         | location     | type       | rating | capacity |
| ---              | ---          | ---        | ---    | ---      |
| str              | str          | str        | i64    | i64      |
|------------------|--------------|------------|--------|----------|
|   7burgers       | Wakey Wakey  | restaurant | 5      | 55       |
| Bang Bang Burger | Forest Rd.   | restaurant | 4      | 55       |
| Costa Coffee     | City Point   | café       | 5      | 41       |
|  Costa Coffee    | The Moorgate | takeaway   | 5      | 0        |
| The Queens Head  | Denman St.   | bar        | 5      | 187      |
Trasformazione dei dati con Polars

Pulizia dei dati testuali

shape: (5, 5)
| business         | location        | type       | rating | capacity |
| ---              | ---             | ---        | ---    | ---      |
| str              | str             | str        | i64    | i64      |
|------------------|-----------------|------------|--------|----------|
|   7burgers       | Wakey Wakey     | restaurant | 5      | 55       |
| Bang Bang Burger | Forest Rd.      | restaurant | 4      | 55       |
| Costa Coffee     | City Point      | café       | 5      | 41       |
|  Costa Coffee    | The Moorgate    | takeaway   | 5      | 0        |
| The Queens Head  | Denman St.      | bar        | 5      | 187      |
Trasformazione dei dati con Polars

Pulizia dei dati testuali

  • .str.contains()
  • .str.strip_chars()
  • .str.strip_chars_start()
  • .str.to_lowercase()
  • ...
Trasformazione dei dati con Polars

Pulizia dei dati testuali

  • .str.contains()
  • .str.strip_chars()
  • .str.strip_chars_start()
  • .str.to_lowercase()
  • ...
1 https://docs.pola.rs/api/python/stable/reference/expressions/string.html
Trasformazione dei dati con Polars

Pulizia dei dati testuali

  • .str.contains()
  • .str.strip_chars()
  • .str.strip_chars_start()
  • .str.to_lowercase()
  • ...
1 https://docs.pola.rs/api/python/stable/reference/expressions/string.html
Trasformazione dei dati con Polars

Pulizia dei dati testuali

ratings.with_columns(

)
Trasformazione dei dati con Polars

Pulizia dei dati testuali

ratings.with_columns(
    pl.col("business")
)
Trasformazione dei dati con Polars

Pulizia dei dati testuali

ratings.with_columns(
    pl.col("business").str.strip_chars_start()
)
shape: (5, 5)
| business         | location     | type       | rating | capacity |
| ---              | ---          | ---        | ---    | ---      |
| str              | str          | str        | i64    | i64      |
|------------------|--------------|------------|--------|----------|
| 7burgers         | Wakey Wakey  | restaurant | 5      | 55       |
| Bang Bang Burger | Forest Rd.   | restaurant | 4      | 55       |
| Costa Coffee     | City Point   | café       | 5      | 41       |
| Costa Coffee     | The Moorgate | takeaway   | 5      | 0        |
| The Queens Head  | Denman St.   | bar        | 5      | 187      |
Trasformazione dei dati con Polars

Combinare dati testuali

shape: (5, 5)
| business         | location        | type       | rating | capacity |
| ---              | ---             | ---        | ---    | ---      |
| str              | str             | str        | i64    | i64      |
|------------------|-----------------|------------|--------|----------|
| 7burgers         | Wakey Wakey     | restaurant | 5      | 55       |
| Bang Bang Burger | Forest Rd.      | restaurant | 4      | 55       |
| Costa Coffee     | City Point      | café       | 5      | 41       |
| Costa Coffee     | The Moorgate    | takeaway   | 5      | 0        |
| The Queens Head  | Denman St.      | bar        | 5      | 187      |
Trasformazione dei dati con Polars

Combinare dati testuali

ratings.with_columns(

)
Trasformazione dei dati con Polars

Combinare dati testuali

ratings.with_columns(
    pl.concat_str(
)
Trasformazione dei dati con Polars

Combinare dati testuali

ratings.with_columns(
    pl.concat_str("business", "location"
)
Trasformazione dei dati con Polars

Combinare dati testuali

ratings.with_columns(
    pl.concat_str("business", "location", separator=":")
)
Trasformazione dei dati con Polars

Combinare dati testuali

ratings.with_columns(
    pl.concat_str("business", "location", separator=":").alias("id")
)
shape: (5, 5)
| business         | location     | type       | ... | id                            |
| ---              | ---          | ---        | --- | ---                           |
| str              | str          | str        | ... | str                           |
|------------------|--------------|------------|-----|-------------------------------|
| 7burgers         | Wakey Wakey  | restaurant | ... | 7burgers:Wakey Wakey          |
| Bang Bang Burger | Forest Rd.   | restaurant | ... | Bang Bang Burger:Forest Rd.   |
| Costa Coffee     | City Point   | café       | ... | Costa Coffee:City Point       |
| Costa Coffee     | The Moorgate | takeaway   | ... | Costa Coffee:The Moorgate     |
| The Queens Head  | Denman St.   | bar        | ... | The Queens Head:Denman St.    |
Trasformazione dei dati con Polars

Ayo berlatih!

Trasformazione dei dati con Polars

Preparing Video For Download...