Recherche et extraction de texte

Transformation de données avec Polars

Liam Brannigan

Data Scientist & Polars Contributor

Recherche de texte

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      |
  • Tâche : trouver "burger" dans le nom de l’entreprise
Transformation de données avec Polars

Recherche de texte

ratings.with_columns(
    pl.col("business")
)
Transformation de données avec Polars

Recherche de texte

ratings.with_columns(
    pl.col("business").str.contains("Burger")
)
Transformation de données avec Polars

Recherche de texte

ratings.with_columns(
    pl.col("business").str.contains("Burger").alias("is_burger")
)
shape: (5, 6)
| business         | location        | type       | rating | capacity | is_burger |
| ---              | ---             | ---        | ---    | ---      | ---       |
| str              | str             | str        | i64    | i64      | bool      |
|------------------|-----------------|------------|--------|----------|-----------|
| 7burgers         | Wakey Wakey     | restaurant | 5      | 55       | false     |
| Bang Bang Burger | Forest Rd.      | restaurant | 4      | 55       | true      |
| Costa Coffee     | City Point      | café       | 5      | 41       | false     |
| Costa Coffee     | The Moorgate    | takeaway   | 5      | 0        | false     |
| The Queens Head  | Denman St.      | bar        | 5      | 187      | false     |
Transformation de données avec Polars

Recherche de texte

ratings.with_columns(
    pl.col("business").str.to_lowercase()
)
Transformation de données avec Polars

Recherche de texte

ratings.with_columns(
    pl.col("business").str.to_lowercase().str.contains("burger").alias("is_burger")
)
shape: (5, 6)
| business         | location        | type       | rating | capacity | is_burger |
| ---              | ---             | ---        | ---    | ---      | ---       |
| str              | str             | str        | i64    | i64      | bool      |
|------------------|-----------------|------------|--------|----------|-----------|
| 7burgers         | Wakey Wakey     | restaurant | 5      | 55       | true     |
| Bang Bang Burger | Forest Rd.      | restaurant | 4      | 55       | true      |
| Costa Coffee     | City Point      | café       | 5      | 41       | false     |
| Costa Coffee     | The Moorgate    | takeaway   | 5      | 0        | false     |
| The Queens Head  | Denman St.      | bar        | 5      | 187      | false     |
Transformation de données avec Polars

Filtrer par correspondance de texte

ratings.filter(

)
Transformation de données avec Polars

Filtrer par correspondance de texte

ratings.filter(
    pl.col("business").str.to_lowercase().str.contains("burger")
)
shape: (5, 6)
| business          | location        | type       | rating | capacity |
| ---               | ---             | ---        | ---    | ---      |
| str               | str             | str        | i64    | i64      |
|-------------------|-----------------|------------|--------|----------|
| 7burgers          | Wakey Wakey     | restaurant | 5      | 55       |
| Bang Bang Burger  | Forest Rd.      | restaurant | 4      | 55       |
| Bronson's Burgers | Arch 112        | takeaway   | 4      | 0        |
| Burger & Lobster  | Bow Bells House | restaurant | 5      | 40       |
| Ted Burgers       | Prince Of Wales | takeaway   | 4      | 0        |
Transformation de données avec Polars

Extraction de texte

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      |
  • Tâche : extraire "burger" ou "coffee" de la colonne business
Transformation de données avec Polars

Extraction de texte

ratings.with_columns(

)
Transformation de données avec Polars

Extraction de texte

ratings.with_columns(
    pl.col("business")
)
Transformation de données avec Polars

Extraction de texte

ratings.with_columns(
    pl.col("business").str.extract("(burger)")
)
Transformation de données avec Polars

Extraction de texte

ratings.with_columns(
    pl.col("business").str.extract("(burger)").alias("food")
)
shape: (5, 6)
| business         | location        | type       | rating | capacity | food   |
| ---              | ---             | ---        | ---    | ---      | ---    |
| str              | str             | str        | i64    | i64      | str    |
|------------------|-----------------|------------|--------|----------|--------|
| 7burgers         | Wakey Wakey     | restaurant | 5      | 55       | burger |
| bang bang burger | Forest Rd.      | restaurant | 4      | 55       | burger |
| costa coffee     | City Point      | café       | 5      | 41       | null   |
| costa coffee     | The Moorgate    | takeaway   | 5      | 0        | null   |
| the queens head  | Denman St.      | bar        | 5      | 187      | null   |
Transformation de données avec Polars

Extraire plusieurs termes

ratings.with_columns(
    pl.col("business").str.extract("(burger|coffee)").alias("food")
)
shape: (5, 6)
| business         | location        | type       | rating | capacity | burger_name |
| ---              | ---             | ---        | ---    | ---      | ---         |
| str              | str             | str        | i64    | i64      | str         |
|------------------|-----------------|------------|--------|----------|-------------|
| 7burgers         | Wakey Wakey     | restaurant | 5      | 55       | burger      |
| bang bang burger | Forest Rd.      | restaurant | 4      | 55       | burger      |
| costa coffee     | City Point      | café       | 5      | 41       | coffee      |
| costa coffee     | The Moorgate    | takeaway   | 5      | 0        | coffee      |
| the queens head  | Denman St.      | bar        | 5      | 187      | null        |
Transformation de données avec Polars

Remplacer du texte

shape: (5, 6)
| business         | location        | type       | rating | capacity | food    |
| ---              | ---             | ---        | ---    | ---      | ---     |
| str              | str             | str        | i64    | i64      | str     |
|------------------|-----------------|------------|--------|----------|---------|
| 7burgers         | Wakey Wakey     | restaurant | 5      | 55       | burger  |
| bang bang burger | Forest Rd.      | restaurant | 4      | 55       | burger  |
| costa coffee     | City Point      | café       | 5      | 41       | coffee  |
| costa coffee     | The Moorgate    | takeaway   | 5      | 0        | coffee  |
| the queens head  | Denman St.      | bar        | 5      | 187      | null    |
  • Tâche : remplacer "Rd." par "Road"
Transformation de données avec Polars

Remplacer du texte

ratings.with_columns(
    pl.col("location")
)
Transformation de données avec Polars

Remplacer du texte

ratings.with_columns(
    pl.col("location").str.replace("Rd.", "Road")
)
shape: (5, 6)
| business         | location        | type       | rating | capacity | food    |
| ---              | ---             | ---        | ---    | ---      | ---     |
| str              | str             | str        | i64    | i64      | str     |
|------------------|-----------------|------------|--------|----------|---------|
| 7burgers         | Wakey Wakey     | restaurant | 5      | 55       | burger  |
| bang bang burger | Forest Road     | restaurant | 4      | 55       | burger  |
| costa coffee     | City Point      | café       | 5      | 41       | coffee  |
| costa coffee     | The Moorgate    | takeaway   | 5      | 0        | coffee  |
| the queens head  | Denman St.      | bar        | 5      | 187      | null    |
Transformation de données avec Polars

Remplacer plusieurs chaînes

ratings.with_columns(
    pl.col("location").str.replace("Rd.", "Road")
)
ratings.with_columns(
    pl.col("location").str.replace_many(                               )
)
Transformation de données avec Polars

Remplacer plusieurs chaînes

ratings.with_columns(
    pl.col("location").str.replace("Rd.", "Road")
)
ratings.with_columns(
    pl.col("location").str.replace_many(["Rd.", "St."],                 )
)
Transformation de données avec Polars

Remplacer plusieurs chaînes

ratings.with_columns(
    pl.col("location").str.replace("Rd.", "Road")
)
ratings.with_columns(
    pl.col("location").str.replace_many(["Rd.", "St."], ["Road", "Street"])
)
Transformation de données avec Polars

Remplacer plusieurs chaînes

shape: (5, 6)
| business         | location        | type       | rating | capacity | food    |
| ---              | ---             | ---        | ---    | ---      | ---     |
| str              | str             | str        | i64    | i64      | str     |
|------------------|-----------------|------------|--------|----------|---------|
| 7burgers         | Wakey Wakey     | restaurant | 5      | 55       | burger  |
| bang bang burger | Forest Road     | restaurant | 4      | 55       | burger  |
| costa coffee     | City Point      | café       | 5      | 41       | coffee  |
| costa coffee     | The Moorgate    | takeaway   | 5      | 0        | coffee  |
| the queens head  | Denman Street   | bar        | 5      | 187      | null    |
Transformation de données avec Polars

Passons à la pratique !

Transformation de données avec Polars

Preparing Video For Download...