Czyszczenie danych tekstowych

Transformacje danych z Polars

Liam Brannigan

Data Scientist & Polars Contributor

Twój instruktor

$$

$$

  • Liam Brannigan, Lead Data Scientist
  • Specjalista ds. ML i inżynierii danych
  • Współtwórca Polars

Zdjęcie profilowe instruktora.

Transformacje danych z Polars

Silnik transformacji

Animacja

Transformacje danych z Polars

Czy ten kurs jest dla ciebie?

$$

  • Tworzenie DataFrame w Polars

$$

  • Używanie wyrażeń Polars

$$

  • Agregacja z group-by

Strona kursu Introduction to Polars

Transformacje danych z Polars

Rozdział 1

Diagram czyszczenia danych tekstowych

Transformacje danych z Polars

Rozdział 2

Dane szeregów czasowych

Transformacje danych z Polars

Rozdział 3

Łączenie DataFrames

Transformacje danych z Polars

Rozdział 4

Własne przepływy pracy i korelacja

Transformacje danych z Polars

Nasz zbiór danych

import polars as pl

ratings = pl.read_csv("restaurant_ratings.csv")
shape: (5, 5)
| business         | location     | type       | rating | capacity |
| ---              | ---          | ---        | ---    | ---      |
| str              | str          | str        | f64    | f64      |
|------------------|--------------|------------|--------|----------|
|   7burgers       | Wakey Wakey  | restaurant | 5.0    | 55.0     |
| Bang Bang Burger | Forest Rd.   | restaurant | 4.0    | 55.0     |
| Costa Coffee     | City Point   | café       | 5.0    | 41.0     |
|  Costa Coffee    | The Moorgate | takeaway   | 5.0    | 0.0      |
| The Queens Head  | Denman St.   | bar        | 5.0    | 187.0    |
Transformacje danych z Polars

Aplikacja do polecania restauracji

shape: (3, 5)
| business         | location     | type       | rating | capacity |
| ---              | ---          | ---        | ---    | ---      |
| str              | str          | str        | f64    | f64      |
|------------------|--------------|------------|--------|----------|
|   7burgers       | Wakey Wakey  | restaurant | 5.0    | 55.0     |
| Bang Bang Burger | Forest Rd.   | restaurant | 4.0    | 55.0     |
| Costa Coffee     | City Point   | café       | 5.0    | 41.0     |
  • Usuń białe znaki
  • Przekonwertuj kolumny rating i capacity
  • Utwórz kolumnę z unikalnym identyfikatorem
Transformacje danych z Polars

Rzutowanie dtype wyrażeniem

ratings.with_columns(

)
Transformacje danych z Polars

Rzutowanie dtype wyrażeniem

ratings.with_columns(
    pl.col("rating").cast(pl.Int64)
)
shape: (5, 5)
| business         | location     | type       | rating | capacity |
| ---              | ---          | ---        | ---    | ---      |
| str              | str          | str        | i64    | f64      |
|------------------|--------------|------------|--------|----------|
|   7burgers       | Wakey Wakey  | restaurant | 5      | 55.0     |
| Bang Bang Burger | Forest Rd.   | restaurant | 4      | 55.0     |
| Costa Coffee     | City Point   | café       | 5      | 41.0     |
|  Costa Coffee    | The Moorgate | takeaway   | 5      | 0.0      |
| The Queens Head  | Denman St.   | bar        | 5      | 187.0    |
Transformacje danych z Polars

Rzutowanie wielu kolumn

ratings.cast(                      )
Transformacje danych z Polars

Rzutowanie wielu kolumn

ratings.cast({                    })
Transformacje danych z Polars

Rzutowanie wielu kolumn

ratings.cast({pl.Float64: pl.Int64})
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      |
Transformacje danych z Polars

Czyszczenie danych tekstowych

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      |
Transformacje danych z Polars

Czyszczenie danych tekstowych

  • .str.contains()
  • .str.strip_chars()
  • .str.strip_chars_start()
  • .str.to_lowercase()
  • ...
Transformacje danych z Polars

Czyszczenie danych tekstowych

  • .str.contains()
  • .str.strip_chars()
  • .str.strip_chars_start()
  • .str.to_lowercase()
  • ...
1 https://docs.pola.rs/api/python/stable/reference/expressions/string.html
Transformacje danych z Polars

Czyszczenie danych tekstowych

  • .str.contains()
  • .str.strip_chars()
  • .str.strip_chars_start()
  • .str.to_lowercase()
  • ...
1 https://docs.pola.rs/api/python/stable/reference/expressions/string.html
Transformacje danych z Polars

Czyszczenie danych tekstowych

ratings.with_columns(

)
Transformacje danych z Polars

Czyszczenie danych tekstowych

ratings.with_columns(
    pl.col("business")
)
Transformacje danych z Polars

Czyszczenie danych tekstowych

ratings.with_columns(
    pl.col("business").str.strip_chars_start()
)
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      |
Transformacje danych z Polars

Łączenie danych tekstowych

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      |
Transformacje danych z Polars

Łączenie danych tekstowych

ratings.with_columns(

)
Transformacje danych z Polars

Łączenie danych tekstowych

ratings.with_columns(
    pl.concat_str(
)
Transformacje danych z Polars

Łączenie danych tekstowych

ratings.with_columns(
    pl.concat_str("business", "location"
)
Transformacje danych z Polars

Łączenie danych tekstowych

ratings.with_columns(
    pl.concat_str("business", "location", separator=":")
)
Transformacje danych z Polars

Łączenie danych tekstowych

ratings.with_columns(
    pl.concat_str("business", "location", separator=":").alias("id")
)
shape: (5, 5)
| business         | location     | type       | ... | id                            |
| ---              | ---          | ---        | --- | ---                           |
| str              | str          | str        | ... | str                           |
|------------------|--------------|------------|-----|-------------------------------|
| 7burgers         | Wakey Wakey  | restaurant | ... | 7burgers:Wakey Wakey          |
| Bang Bang Burger | Forest Rd.   | restaurant | ... | Bang Bang Burger:Forest Rd.   |
| Costa Coffee     | City Point   | café       | ... | Costa Coffee:City Point       |
| Costa Coffee     | The Moorgate | takeaway   | ... | Costa Coffee:The Moorgate     |
| The Queens Head  | Denman St.   | bar        | ... | The Queens Head:Denman St.    |
Transformacje danych z Polars

Czas na praktykę!

Transformacje danych z Polars

Preparing Video For Download...