Xử lý dữ liệu mảng

Mở rộng và tối ưu hóa pipeline dữ liệu với Polars

Liam Brannigan

Data Scientist & Polars Contributor

Dữ liệu mảng

Sơ đồ bảng có cột tiêu đề sự kiện và cột mảng thẻ

Mở rộng và tối ưu hóa pipeline dữ liệu với Polars

Tập dữ liệu sự kiện

events = pl.read_parquet("chicago_events.parquet")
Mở rộng và tối ưu hóa pipeline dữ liệu với Polars

Tập dữ liệu sự kiện

events.select(
    "event_date", "event_title", "tags", "area"
).head()
shape: (5, 4)
| event_date | event_title      | tags                                | area          |
| ---        | ---              | ---                                 | ---           |
| date       | str              | list[str]                           | str           |
|------------|------------------|-------------------------------------|---------------|
| 2025-06-14 | Chef Showcase    | ["food", "chef_demo"]               | West Loop     |
| 2025-06-29 | Folk Festival    | ["music", "neighborhood", "crafts"] | Andersonville |
| 2025-05-25 | Fireworks Night  | ["nightlife", "family"]             | Downtown      |
| 2025-09-20 | Rail History Day | ["history", "family", "education"]  | Pullman       |
| 2025-07-04 | Grant Park Concert | ["music", "holiday", "family"]    | Loop          |
Mở rộng và tối ưu hóa pipeline dữ liệu với Polars

Thẻ của sự kiện

events["tags"][0]
shape: (2,)
Series: '' [str]
[
    "food"
    "chef_demo"
]
Mở rộng và tối ưu hóa pipeline dữ liệu với Polars

Lấy thẻ chính

events.select(
    "event_title",
    "tags",

)
Mở rộng và tối ưu hóa pipeline dữ liệu với Polars

Lấy thẻ chính

events.select(
    "event_title",
    "tags",
    pl.col("tags").list.get(0),
)
Mở rộng và tối ưu hóa pipeline dữ liệu với Polars

Lấy thẻ chính

events.select(
    "event_title",
    "tags",
    pl.col("tags").list.get(0).alias("primary_tag"),
)
Mở rộng và tối ưu hóa pipeline dữ liệu với Polars

Lấy thẻ chính

events.select(
    "event_title",
    "tags",
    pl.col("tags").list.get(0).alias("primary_tag"),
).head(3)
shape: (3, 3)
| event_title        | tags                                | primary_tag |
| ---                | ---                                 | ---         |
| str                | list[str]                           | str         |
|--------------------|-------------------------------------|-------------|
| Chef Showcase      | ["food", "chef_demo"]               | food        |
| Folk Festival      | ["music", "neighborhood", "crafts"] | music       |
| Fireworks Night    | ["nightlife", "family"]             | nightlife   |
Mở rộng và tối ưu hóa pipeline dữ liệu với Polars

Phân tích đặc trưng sự kiện

events.select(
    "event_title","tags",


)
Mở rộng và tối ưu hóa pipeline dữ liệu với Polars

Phân tích đặc trưng sự kiện

events.select(
    "event_title","tags",
    pl.col("tags").list.len().alias("tag_count"),

)
Mở rộng và tối ưu hóa pipeline dữ liệu với Polars

Phân tích đặc trưng sự kiện

events.select(
    "event_title","tags",
    pl.col("tags").list.len().alias("tag_count"),
    pl.col("tags").list.contains("family").alias("has_family"),
)
Mở rộng và tối ưu hóa pipeline dữ liệu với Polars

Sự kiện thân thiện gia đình

events.select(
    "event_title","tags",
    pl.col("tags").list.len().alias("tag_count"),
    pl.col("tags").list.contains("family").alias("has_family"),
).filter(pl.col("has_family")).head(3)
shape: (3, 4)
| event_title        | tags                               | tag_count | has_family |
| ---                | ---                                | ---       | ---        |
| str                | list[str]                          | u32       | bool       |
|--------------------|------------------------------------|-----------|------------|
| Fireworks Night    | ["nightlife", "family"]            | 2         | true       |
| Rail History Day   | ["history", "family", "education"] | 3         | true       |
| Grant Park Concert | ["music", "holiday", "family"]     | 3         | true       |
Mở rộng và tối ưu hóa pipeline dữ liệu với Polars

Biểu thức list trong Polars

  • list.first() → chỉ số

$$

  • list.unique() → loại trùng

$$

  • list.mean() → tổng hợp số

$$

  • Còn nhiều trong tài liệu 🔥
1 https://docs.pola.rs/api/python/stable/reference/expressions/list.html
Mở rộng và tối ưu hóa pipeline dữ liệu với Polars

Nổ cột tags

events.select("event_title","tags")
Mở rộng và tối ưu hóa pipeline dữ liệu với Polars

Nổ cột tags

events.select("event_title","tags").explode("tags")
Mở rộng và tối ưu hóa pipeline dữ liệu với Polars

Tags sau khi nổ

events.select("event_title","tags").explode("tags")
shape: (1346328, 2)
| event_title        | tags         |
| ---                | ---          |
| str                | str          |
|--------------------|------------- |
| Chef Showcase      | food         |
| Chef Showcase      | chef_demo    |
| Folk Festival      | music        |
| Folk Festival      | neighborhood |
| Folk Festival      | crafts       |
| Fireworks Night    | nightlife    |
| ...                | ...          |
Mở rộng và tối ưu hóa pipeline dữ liệu với Polars

Đếm độ phổ biến của tag

events.select("event_title","tags").explode("tags")["tags"].value_counts(sort=True)
Mở rộng và tối ưu hóa pipeline dữ liệu với Polars

Đếm độ phổ biến của tag

events.select("event_title","tags").explode("tags")["tags"].value_counts(sort=True).head()
shape: (5, 2)
| tags      | count   |
| ---       | ---     |
| str       | u32     |
|-----------|---------|
| family    | 59864   |
| nightlife | 45302   |
| music     | 35612   |
| food      | 23948   |
| culture   | 21566   |
Mở rộng và tối ưu hóa pipeline dữ liệu với Polars

Ayo berlatih!

Mở rộng và tối ưu hóa pipeline dữ liệu với Polars

Preparing Video For Download...