搜尋與擷取文字

使用 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   | 5      | 0        |
| The Queens Head  | Denman St.   | bar        | 5      | 187      |
  • 任務:在商家名稱中尋找 "burger"
使用 Polars 進行資料轉換

搜尋文字

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

搜尋文字

ratings.with_columns(
    pl.col("business").str.contains("Burger")
)
使用 Polars 進行資料轉換

搜尋文字

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

搜尋文字

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

搜尋文字

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

篩選文字相符項目

ratings.filter(

)
使用 Polars 進行資料轉換

篩選文字相符項目

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        |
使用 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   | 5      | 0        |
| the queens head  | Denman St.   | bar        | 5      | 187      |
  • 任務:從 business 欄位擷取 "burger""coffee"
使用 Polars 進行資料轉換

擷取文字

ratings.with_columns(

)
使用 Polars 進行資料轉換

擷取文字

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

擷取文字

ratings.with_columns(
    pl.col("business").str.extract("(burger)")
)
使用 Polars 進行資料轉換

擷取文字

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

擷取文字—多個詞彙

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

取代文字

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    |
  • 任務:將 "Rd." 取代為 "Road"
使用 Polars 進行資料轉換

取代文字

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

取代文字

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

取代多個字串

ratings.with_columns(
    pl.col("location").str.replace("Rd.", "Road")
)
ratings.with_columns(
    pl.col("location").str.replace_many(                               )
)
使用 Polars 進行資料轉換

取代多個字串

ratings.with_columns(
    pl.col("location").str.replace("Rd.", "Road")
)
ratings.with_columns(
    pl.col("location").str.replace_many(["Rd.", "St."],                 )
)
使用 Polars 進行資料轉換

取代多個字串

ratings.with_columns(
    pl.col("location").str.replace("Rd.", "Road")
)
ratings.with_columns(
    pl.col("location").str.replace_many(["Rd.", "St."], ["Road", "Street"])
)
使用 Polars 進行資料轉換

取代多個字串

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

一起來練習吧!

使用 Polars 進行資料轉換

Preparing Video For Download...