Voorwaardelijke expressies maken

Data transformatie met Polars

Liam Brannigan

Data Scientist & Polars Contributor

Indelen op beoordelingskwaliteit

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      |
  • Taak: identificeer hoog beoordeelde restaurants
Data transformatie met Polars

Indelen op beoordelingskwaliteit

ratings.with_columns(


)
Data transformatie met Polars

Indelen op beoordelingskwaliteit

ratings.with_columns(
    pl.when(                     )

)
Data transformatie met Polars

Indelen op beoordelingskwaliteit

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

)
Data transformatie met Polars

Indelen op beoordelingskwaliteit

ratings.with_columns(
    pl.when(pl.col("rating") == 5).then(pl.lit("Hoog beoordeeld"))

)

$$

$$

  • Gebruik pl.lit() om een letterlijke waarde door te geven, geen kolomnaam
Data transformatie met Polars

Indelen op beoordelingskwaliteit

ratings.with_columns(
    pl.when(pl.col("rating") == 5).then(pl.lit("Hoog beoordeeld"))
    .otherwise(pl.lit("Verbetering nodig"))
)
Data transformatie met Polars

Indelen op beoordelingskwaliteit

ratings.with_columns(
    pl.when(pl.col("rating") == 5).then(pl.lit("Hoog beoordeeld"))
    .otherwise(pl.lit("Verbetering nodig")).alias("quality")
)
Data transformatie met Polars

Indelen op beoordelingskwaliteit

ratings.with_columns(
    pl.when(pl.col("rating") == 5).then(pl.lit("Hoog beoordeeld"))
    .otherwise(pl.lit("Verbetering nodig")).alias("quality")
)
shape: (5, 6)
| business         | location        | type       | rating | capacity | quality            |
| ---              | ---             | ---        | ---    | ---      | ---                |
| str              | str             | str        | i64    | i64      | str                |
|------------------|-----------------|------------|--------|----------|--------------------|
| 7burgers         | Wakey Wakey     | restaurant | 5      | 55       | Hoog beoordeeld    |
| Bang Bang Burger | Forest Rd.      | restaurant | 4      | 55       | Verbetering nodig  |
| ...              | ...             | ...        | ...    | ...      | ...                |
Data transformatie met Polars

Indelen op locatiegrootte

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      |
  • Taak: filter restaurants op capaciteit
Data transformatie met Polars

Indelen op locatiegrootte

ratings.with_columns(



)
Data transformatie met Polars

Indelen op locatiegrootte

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


)
Data transformatie met Polars

Indelen op locatiegrootte

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


)
Data transformatie met Polars

Indelen op locatiegrootte

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

)
Data transformatie met Polars

Indelen op locatiegrootte

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

)
Data transformatie met Polars

Indelen op locatiegrootte

ratings.with_columns(
    pl.when(pl.col("capacity") > 100).then(pl.lit("Groot"))
    .when(pl.col("capacity") >= 20).then(pl.lit("Middel"))
    .otherwise(pl.lit("Klein"))
)
Data transformatie met Polars

Indelen op locatiegrootte

ratings.with_columns(
    pl.when(pl.col("capacity") > 100).then(pl.lit("Groot"))
    .when(pl.col("capacity") >= 20).then(pl.lit("Middel"))
    .otherwise(pl.lit("Klein")).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       | Middel     |
| Costa Coffee     | The Moorgate    | takeaway   | 3      | 0        | Klein      |
| The Queens Head  | Denman St.      | bar        | 5      | 187      | Groot      |
Data transformatie met Polars

Bedrijfstypen standaardiseren

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      |
  • Taak: standaardiseer de kolom type
Data transformatie met Polars

Bedrijfstypen standaardiseren

ratings.with_columns(
    pl.col("type")
)
Data transformatie met Polars

Bedrijfstypen standaardiseren

ratings.with_columns(
    pl.col("type").replace(                    )
)
Data transformatie met Polars

Bedrijfstypen standaardiseren

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      |
Data transformatie met Polars

Laten we oefenen!

Data transformatie met Polars

Preparing Video For Download...