Bedingte Ausdrücke erstellen

Daten­transformation mit Polars

Liam Brannigan

Data Scientist & Polars Contributor

Nach Bewertungsqualität kategorisieren

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      |
  • Aufgabe: Top-bewertete Restaurants finden
Daten­transformation mit Polars

Nach Bewertungsqualität kategorisieren

ratings.with_columns(


)
Daten­transformation mit Polars

Nach Bewertungsqualität kategorisieren

ratings.with_columns(
    pl.when(                     )

)
Daten­transformation mit Polars

Nach Bewertungsqualität kategorisieren

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

)
Daten­transformation mit Polars

Nach Bewertungsqualität kategorisieren

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

)

$$

$$

  • Verwende pl.lit(), um einen Literalwert zu übergeben, keinen Spaltennamen
Daten­transformation mit Polars

Nach Bewertungsqualität kategorisieren

ratings.with_columns(
    pl.when(pl.col("rating") == 5).then(pl.lit("Highly Rated"))
    .otherwise(pl.lit("Needs Improvement"))
)
Daten­transformation mit Polars

Nach Bewertungsqualität kategorisieren

ratings.with_columns(
    pl.when(pl.col("rating") == 5).then(pl.lit("Highly Rated"))
    .otherwise(pl.lit("Needs Improvement")).alias("quality")
)
Daten­transformation mit Polars

Nach Bewertungsqualität kategorisieren

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  |
| ...              | ...             | ...        | ...    | ...      | ...                |
Daten­transformation mit Polars

Nach Lokalgröße kategorisieren

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      |
  • Aufgabe: Restaurants nach Kapazität filtern
Daten­transformation mit Polars

Nach Lokalgröße kategorisieren

ratings.with_columns(



)
Daten­transformation mit Polars

Nach Lokalgröße kategorisieren

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


)
Daten­transformation mit Polars

Nach Lokalgröße kategorisieren

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


)
Daten­transformation mit Polars

Nach Lokalgröße kategorisieren

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

)
Daten­transformation mit Polars

Nach Lokalgröße kategorisieren

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

)
Daten­transformation mit Polars

Nach Lokalgröße kategorisieren

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"))
)
Daten­transformation mit Polars

Nach Lokalgröße kategorisieren

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      |
Daten­transformation mit Polars

Betriebstypen standardisieren

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      |
  • Aufgabe: Die Spalte type standardisieren
Daten­transformation mit Polars

Betriebstypen standardisieren

ratings.with_columns(
    pl.col("type")
)
Daten­transformation mit Polars

Betriebstypen standardisieren

ratings.with_columns(
    pl.col("type").replace(                    )
)
Daten­transformation mit Polars

Betriebstypen standardisieren

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      |
Daten­transformation mit Polars

Lass uns üben!

Daten­transformation mit Polars

Preparing Video For Download...