建立條件運算式

使用 Polars 進行資料轉換

Liam Brannigan

Data Scientist & Polars Contributor

依評分品質分類

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      |
  • 任務:找出高評分的餐廳
使用 Polars 進行資料轉換

依評分品質分類

ratings.with_columns(


)
使用 Polars 進行資料轉換

依評分品質分類

ratings.with_columns(
    pl.when(                     )

)
使用 Polars 進行資料轉換

依評分品質分類

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

)
使用 Polars 進行資料轉換

依評分品質分類

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

)

$$

$$

  • 使用 pl.lit() 傳入常值,不是欄名
使用 Polars 進行資料轉換

依評分品質分類

ratings.with_columns(
    pl.when(pl.col("rating") == 5).then(pl.lit("Highly Rated"))
    .otherwise(pl.lit("Needs Improvement"))
)
使用 Polars 進行資料轉換

依評分品質分類

ratings.with_columns(
    pl.when(pl.col("rating") == 5).then(pl.lit("Highly Rated"))
    .otherwise(pl.lit("Needs Improvement")).alias("quality")
)
使用 Polars 進行資料轉換

依評分品質分類

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  |
| ...              | ...             | ...        | ...    | ...      | ...                |
使用 Polars 進行資料轉換

依場地大小分類

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      |
  • 任務:依容量篩選餐廳
使用 Polars 進行資料轉換

依場地大小分類

ratings.with_columns(



)
使用 Polars 進行資料轉換

依場地大小分類

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


)
使用 Polars 進行資料轉換

依場地大小分類

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


)
使用 Polars 進行資料轉換

依場地大小分類

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

)
使用 Polars 進行資料轉換

依場地大小分類

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

)
使用 Polars 進行資料轉換

依場地大小分類

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"))
)
使用 Polars 進行資料轉換

依場地大小分類

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      |
使用 Polars 進行資料轉換

標準化商家類型

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      |
  • 任務:標準化 type 欄位
使用 Polars 進行資料轉換

標準化商家類型

ratings.with_columns(
    pl.col("type")
)
使用 Polars 進行資料轉換

標準化商家類型

ratings.with_columns(
    pl.col("type").replace(                    )
)
使用 Polars 進行資料轉換

標準化商家類型

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      |
使用 Polars 進行資料轉換

一起來練習吧!

使用 Polars 進行資料轉換

Preparing Video For Download...