Limpando dados de texto

Transformação de Dados com Polars

Liam Brannigan

Data Scientist & Polars Contributor

Conheça teu instrutor

$$

$$

  • Liam Brannigan, Lead Data Scientist
  • Especialista em ML e Engenharia de Dados
  • Contribuidor do Polars

Foto de perfil do instrutor.

Transformação de Dados com Polars

Mecanismo de Transformação

Animação

Transformação de Dados com Polars

Este curso é pra você?

$$

  • Criando um DataFrame do Polars

$$

  • Usando uma expressão do Polars

$$

  • Fazendo uma agregação group by

Introdução ao Polars - página do curso

Transformação de Dados com Polars

Capítulo 1

Diagrama de limpeza de texto

Transformação de Dados com Polars

Capítulo 2

Dados de séries temporais

Transformação de Dados com Polars

Capítulo 3

Combinando DataFrames

Transformação de Dados com Polars

Capítulo 4

Workflows customizados e correlação

Transformação de Dados com Polars

Conheça o dataset

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    |
Transformação de Dados com Polars

App de recomendação de restaurantes

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     |
  • Remover espaços em branco
  • Converter colunas rating e capacity
  • Criar coluna de identificador único
Transformação de Dados com Polars

Convertendo dtype com uma expressão

ratings.with_columns(

)
Transformação de Dados com Polars

Convertendo dtype com uma expressão

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    |
Transformação de Dados com Polars

Convertendo várias colunas

ratings.cast(                      )
Transformação de Dados com Polars

Convertendo várias colunas

ratings.cast({                    })
Transformação de Dados com Polars

Convertendo várias colunas

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      |
Transformação de Dados com Polars

Limpando dados de texto

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      |
Transformação de Dados com Polars

Limpando dados de texto

  • .str.contains()
  • .str.strip_chars()
  • .str.strip_chars_start()
  • .str.to_lowercase()
  • ...
Transformação de Dados com Polars

Limpando dados de texto

  • .str.contains()
  • .str.strip_chars()
  • .str.strip_chars_start()
  • .str.to_lowercase()
  • ...
1 https://docs.pola.rs/api/python/stable/reference/expressions/string.html
Transformação de Dados com Polars

Limpando dados de texto

  • .str.contains()
  • .str.strip_chars()
  • .str.strip_chars_start()
  • .str.to_lowercase()
  • ...
1 https://docs.pola.rs/api/python/stable/reference/expressions/string.html
Transformação de Dados com Polars

Limpando dados de texto

ratings.with_columns(

)
Transformação de Dados com Polars

Limpando dados de texto

ratings.with_columns(
    pl.col("business")
)
Transformação de Dados com Polars

Limpando dados de texto

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      |
Transformação de Dados com Polars

Combinando textos

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      |
Transformação de Dados com Polars

Combinando textos

ratings.with_columns(

)
Transformação de Dados com Polars

Combinando textos

ratings.with_columns(
    pl.concat_str(
)
Transformação de Dados com Polars

Combinando textos

ratings.with_columns(
    pl.concat_str("business", "location"
)
Transformação de Dados com Polars

Combinando textos

ratings.with_columns(
    pl.concat_str("business", "location", separator=":")
)
Transformação de Dados com Polars

Combinando textos

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.    |
Transformação de Dados com Polars

Vamos praticar!

Transformação de Dados com Polars

Preparing Video For Download...