Skip to content

Latest commit

 

History

History
75 lines (51 loc) · 1.02 KB

File metadata and controls

75 lines (51 loc) · 1.02 KB

資料處理 (Data Processing)


目錄 (Table of Contents)

  1. ...
  2. ...

uv add polars
import polars as pl

迭代橫行 (Iterating Over Rows)

在 DataFrame 外部溝通:

for row in df.iter_rows(named=True):
    send_email(row["name"]

.to_dicts() 會立即全載,可能吃掉大量記憶體。

在內部 DataFrame 處理:

df.with_columns(
    (pl.col("salary") * 1.03).alias("salary_after_raise"),
)

查找資料

取單筆資料:

# 取第一行
row = df.row(0, named=True)
print(row["name"])

取特定條件的單筆資料:

row = df.filter(pl.col("name") == "Alice").row(0, named=True)
print(row["name"])

篩選:

# 單一條件
df.filter(pl.col("age") > 28)

# 多條件 AND
df.filter((pl.col("age") > 25) & (pl.col("score") >= 88))

# 多條件 OR
df.filter((pl.col("age") < 26) | (pl.col("score") > 90))

# isin 篩選
df.filter(pl.col("name").is_in(["Alice", "Bob"]))