Créer des expressions conditionnelles

Transformation de données avec Polars

Liam Brannigan

Data Scientist & Polars Contributor

Catégoriser par qualité de note

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   | 3      | 0        |
| The Queens Head  | Denman St.      | bar        | 5      | 187      |
  • Tâche : identifier les restaurants très bien notés
Transformation de données avec Polars

Catégoriser par qualité de note

ratings.with_columns(


)
Transformation de données avec Polars

Catégoriser par qualité de note

ratings.with_columns(
    pl.when(                     )

)
Transformation de données avec Polars

Catégoriser par qualité de note

ratings.with_columns(
    pl.when(pl.col("rating") == 5)

)
Transformation de données avec Polars

Catégoriser par qualité de note

ratings.with_columns(
    pl.when(pl.col("rating") == 5).then(pl.lit("Highly Rated"))

)

$$

$$

  • Utilisez pl.lit() pour passer une valeur littérale, pas un nom de colonne
Transformation de données avec Polars

Catégoriser par qualité de note

ratings.with_columns(
    pl.when(pl.col("rating") == 5).then(pl.lit("Highly Rated"))
    .otherwise(pl.lit("Needs Improvement"))
)
Transformation de données avec Polars

Catégoriser par qualité de note

ratings.with_columns(
    pl.when(pl.col("rating") == 5).then(pl.lit("Highly Rated"))
    .otherwise(pl.lit("Needs Improvement")).alias("quality")
)
Transformation de données avec Polars

Catégoriser par qualité de note

ratings.with_columns(
    pl.when(pl.col("rating") == 5).then(pl.lit("Highly Rated"))
    .otherwise(pl.lit("Needs Improvement")).alias("quality")
)
shape: (5, 6)
| business         | location        | type       | rating | capacity | quality            |
| ---              | ---             | ---        | ---    | ---      | ---                |
| str              | str             | str        | i64    | i64      | str                |
|------------------|-----------------|------------|--------|----------|--------------------|
| 7burgers         | Wakey Wakey     | restaurant | 5      | 55       | Highly Rated       |
| Bang Bang Burger | Forest Rd.      | restaurant | 4      | 55       | Needs Improvement  |
| ...              | ...             | ...        | ...    | ...      | ...                |
Transformation de données avec Polars

Catégoriser par taille du lieu

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   | 3      | 0        |
| The Queens Head  | Denman St.      | bar        | 5      | 187      |
  • Tâche : filtrer les restaurants par capacité
Transformation de données avec Polars

Catégoriser par taille du lieu

ratings.with_columns(



)
Transformation de données avec Polars

Catégoriser par taille du lieu

ratings.with_columns(
    pl.when(pl.col("capacity") > 100)


)
Transformation de données avec Polars

Catégoriser par taille du lieu

ratings.with_columns(
    pl.when(pl.col("capacity") > 100).then(pl.lit("Large"))


)
Transformation de données avec Polars

Catégoriser par taille du lieu

ratings.with_columns(
    pl.when(pl.col("capacity") > 100).then(pl.lit("Large"))
    .when(pl.col("capacity") >= 20)

)
Transformation de données avec Polars

Catégoriser par taille du lieu

ratings.with_columns(
    pl.when(pl.col("capacity") > 100).then(pl.lit("Large"))
    .when(pl.col("capacity") >= 20).then(pl.lit("Medium"))

)
Transformation de données avec Polars

Catégoriser par taille du lieu

ratings.with_columns(
    pl.when(pl.col("capacity") > 100).then(pl.lit("Large"))
    .when(pl.col("capacity") >= 20).then(pl.lit("Medium"))
    .otherwise(pl.lit("Small"))
)
Transformation de données avec Polars

Catégoriser par taille du lieu

ratings.with_columns(
    pl.when(pl.col("capacity") > 100).then(pl.lit("Large"))
    .when(pl.col("capacity") >= 20).then(pl.lit("Medium"))
    .otherwise(pl.lit("Small")).alias("venue_size")
)
shape: (3, 6)
| business         | location        | type       | rating | capacity | venue_size |
| ---              | ---             | ---        | ---    | ---      | ---        |
| str              | str             | str        | i64    | i64      | str        |
|------------------|-----------------|------------|--------|----------|------------|
| 7burgers         | Wakey Wakey     | restaurant | 5      | 55       | Medium     |
| Costa Coffee     | The Moorgate    | takeaway   | 3      | 0        | Small      |
| The Queens Head  | Denman St.      | bar        | 5      | 187      | Large      |
Transformation de données avec Polars

Standardiser les types d’établissements

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   | 3      | 0        |
| The Queens Head  | Denman St.      | bar        | 5      | 187      |
  • Tâche : standardiser la colonne type
Transformation de données avec Polars

Standardiser les types d’établissements

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

Standardiser les types d’établissements

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

Standardiser les types d’établissements

ratings.with_columns(
    pl.col("type").replace("café", "restaurant")
)
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      | restaurant | 5      | 41       |
| Costa Coffee     | The Moorgate    | takeaway   | 3      | 0        |
| The Queens Head  | Denman St.      | bar        | 5      | 187      |
Transformation de données avec Polars

Passons à la pratique !

Transformation de données avec Polars

Preparing Video For Download...