텍스트 데이터 정리

Polars로 하는 데이터 변환

Liam Brannigan

Data Scientist & Polars Contributor

강사 소개

$$

$$

  • 리암 브래니건(Liam Brannigan), 수석 데이터 과학자
  • ML 및 데이터 엔지니어링 전문가
  • Polars 기여자

강사 프로필 사진.

Polars로 하는 데이터 변환

변환 엔진

애니메이션

Polars로 하는 데이터 변환

이 과정이 맞으신가요?

$$

  • Polars DataFrame 생성

$$

  • Polars 표현식 사용

$$

  • 그룹화 집계 수행

Polars 입문 - 코스 페이지

Polars로 하는 데이터 변환

챕터 1

텍스트 데이터 정리 다이어그램

Polars로 하는 데이터 변환

챕터 2

시계열 데이터

Polars로 하는 데이터 변환

챕터 3

DataFrame 결합

Polars로 하는 데이터 변환

챕터 4

맞춤형 워크플로우와 상관관계

Polars로 하는 데이터 변환

데이터셋 소개

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    |
Polars로 하는 데이터 변환

레스토랑 추천 앱

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     |
  • 공백 제거
  • rating과 capacity 열 변환
  • 고유 ID 열 생성
Polars로 하는 데이터 변환

식별자 타입 캐스팅(표현식)

ratings.with_columns(

)
Polars로 하는 데이터 변환

식별자 타입 캐스팅(표현식)

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    |
Polars로 하는 데이터 변환

여러 열 캐스팅

ratings.cast(                      )
Polars로 하는 데이터 변환

여러 열 캐스팅

ratings.cast({                    })
Polars로 하는 데이터 변환

여러 열 캐스팅

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      |
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      |
Polars로 하는 데이터 변환

텍스트 데이터 정리

  • .str.contains()
  • .str.strip_chars()
  • .str.strip_chars_start()
  • .str.to_lowercase()
  • ...
Polars로 하는 데이터 변환

텍스트 데이터 정리

  • .str.contains()
  • .str.strip_chars()
  • .str.strip_chars_start()
  • .str.to_lowercase()
  • ...
1 https://docs.pola.rs/api/python/stable/reference/expressions/string.html
Polars로 하는 데이터 변환

텍스트 데이터 정리

  • .str.contains()
  • .str.strip_chars()
  • .str.strip_chars_start()
  • .str.to_lowercase()
  • ...
1 https://docs.pola.rs/api/python/stable/reference/expressions/string.html
Polars로 하는 데이터 변환

텍스트 데이터 정리

ratings.with_columns(

)
Polars로 하는 데이터 변환

텍스트 데이터 정리

ratings.with_columns(
    pl.col("business")
)
Polars로 하는 데이터 변환

텍스트 데이터 정리

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      |
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      |
Polars로 하는 데이터 변환

텍스트 데이터 결합

ratings.with_columns(

)
Polars로 하는 데이터 변환

텍스트 데이터 결합

ratings.with_columns(
    pl.concat_str(
)
Polars로 하는 데이터 변환

텍스트 데이터 결합

ratings.with_columns(
    pl.concat_str("business", "location"
)
Polars로 하는 데이터 변환

텍스트 데이터 결합

ratings.with_columns(
    pl.concat_str("business", "location", separator=":")
)
Polars로 하는 데이터 변환

텍스트 데이터 결합

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.    |
Polars로 하는 데이터 변환

연습해 봅시다!

Polars로 하는 데이터 변환

Preparing Video For Download...