Limpieza de texto

Transformación de datos con Polars

Liam Brannigan

Data Scientist & Polars Contributor

Conoce a tu instructor

$$

$$

  • Liam Brannigan, Lead Data Scientist
  • Especialista en ML e ingeniería de datos
  • Colaborador de Polars

Foto de perfil del instructor.

Transformación de datos con Polars

Motor de transformación

Animación

Transformación de datos con Polars

¿Es este curso para ti?

$$

  • Crear un DataFrame de Polars

$$

  • Usar una expresión de Polars

$$

  • Hacer una agregación group-by

Introducción a Polars - página del curso

Transformación de datos con Polars

Capítulo 1

Diagrama de limpieza de texto

Transformación de datos con Polars

Capítulo 2

Datos de series temporales

Transformación de datos con Polars

Capítulo 3

Combinación de DataFrames

Transformación de datos con Polars

Capítulo 4

Flujos de trabajo personalizados y correlación

Transformación de datos con Polars

Conoce el 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    |
Transformación de datos con Polars

App de recomendación 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     |
  • Quitar espacios en blanco
  • Convertir columnas rating y capacity
  • Crear columna de identificador único
Transformación de datos con Polars

Cambiar dtype con una expresión

ratings.with_columns(

)
Transformación de datos con Polars

Cambiar dtype con una expresión

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    |
Transformación de datos con Polars

Cambiar varias columnas

ratings.cast(                      )
Transformación de datos con Polars

Cambiar varias columnas

ratings.cast({                    })
Transformación de datos con Polars

Cambiar varias columnas

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      |
Transformación de datos con Polars

Limpieza 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      |
Transformación de datos con Polars

Limpieza de texto

  • .str.contains()
  • .str.strip_chars()
  • .str.strip_chars_start()
  • .str.to_lowercase()
  • ...
Transformación de datos con Polars

Limpieza 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
Transformación de datos con Polars

Limpieza 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
Transformación de datos con Polars

Limpieza de texto

ratings.with_columns(

)
Transformación de datos con Polars

Limpieza de texto

ratings.with_columns(
    pl.col("business")
)
Transformación de datos con Polars

Limpieza 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      |
Transformación de datos con Polars

Combinar 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      |
Transformación de datos con Polars

Combinar texto

ratings.with_columns(

)
Transformación de datos con Polars

Combinar texto

ratings.with_columns(
    pl.concat_str(
)
Transformación de datos con Polars

Combinar texto

ratings.with_columns(
    pl.concat_str("business", "location"
)
Transformación de datos con Polars

Combinar texto

ratings.with_columns(
    pl.concat_str("business", "location", separator=":")
)
Transformación de datos con Polars

Combinar texto

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.    |
Transformación de datos con Polars

¡Vamos a practicar!

Transformación de datos con Polars

Preparing Video For Download...